Mongodb文档中的搜索键

时间:2015-07-17 09:27:34

标签: mongodb

以下是我在mongodb中插入的json。我需要找到持卡人价值为200的记录

 db.test3.find({"message1":{$eq:true}})
> db.test3.find({"100":{$eq:true}})
> db.test3.find({cardholders:{$eq:'100'}})
> db.test3.find({cardholders:{$eq:100}})

请指教。我有以下

1. name
2. username
3. email

2 个答案:

答案 0 :(得分:1)

您基本上希望在find()查询中包含 projection 参数,该参数匹配仅包含投影字段和_id字段的文档。在这种情况下,由于"200"是关键,您可以将其投影并使用 map() 光标获取值,如下所示:

db.test3.find(
    {}, 
    {"cardholders.200": 1}
).map(function(doc){ return doc["cardholders"]["200"]})

<强>输出

/* 0 */
{
    "0" : [ 
        {
            "message" : "message1",
            "time" : "timestamp"
        }, 
        {
            "message" : "message2",
            "time" : "timestamp"
        }, 
        {
            "message" : "message3",
            "time" : "timestamp"
        }
    ]
}

<强>更新

为了使查询更容易,我建议您更改架构,将持卡人密钥更改为包含嵌入文档的数组。这些嵌入式文档将具有键和值字段; key字段包含以前的动态键,value字段包含数组值:

{
  "_id": "11",
  "cardholders": [
    {
        "key": "100",
        "values": [
            {
                "message": "message1",
                "time": "timestamp"
            },
            {
                "message": "message2",
                "time": "timestamp"
            }
        ]
    },
    {
        "key": "200",
        "values": [
            {
                "message": "message1",
                "time": "timestamp"
            },
            {
                "message": "message2",
                "time": "timestamp"
            }
        ]
    },
    {
        "key": "300",
        "values": [
            {
                "message": "message1",
                "time": "timestamp"
            },
            {
                "message": "message2",
                "time": "timestamp"
            }
        ]
    }
  ]
}

然后,您可以使用 dot notation $elemMatch 投影的组合查询嵌入文档,如下所示:

db.test3.find(
    {"cardholders.key": "200"}, 
    { 
        "cardholders": { 
            "$elemMatch": { "key": "200" } 
        }
    }
);

答案 1 :(得分:0)

我认为无法搜索密钥。

编辑:在持卡人身上使用$ exists运算符。(值)例如

db.test3.find({
  "cardholders.200": {
    "$exists": True
  }})

OLD:也许改变一下:

 {
  "_id": "11",
  "cardholders": [
    {
      "array": [
        {
          "message": "message1",
          "time": "timestamp"
        },
        {
          "message": "message2",
          "time": "timestamp"
        }],
      "value": "100" (or 100)
    },
    {
      "array": [
        {
          "message": "message1",
          "time": "timestamp"
        },
        {
          "message": "message2",
          "time": "timestamp"
        },
        {
          "message": "message3",
          "time": "timestamp"
        }],
      "value": "200" (or 200)
    },
    {
      "array": [
        {
          "message": "message1",
          "time": "timestamp"
        },
        {
          "message": "message2",
          "time": "timestamp"
        }],
      "value": "300" (or 300)
    }
  ]
}

然后索引类型= 1的索引cardholders.value 并使用以下内容:

 > db.test3.find({"cardholders.value": "200"})

(注意:请使用您的编程语言标记您的问题,如果有的话。)