当前位置: 动力学知识库 > 问答 > 编程问答 >

Search key in Mongodb document

问题描述:

following is my json which i have inserted in mongodb. I need to find record where cardholders value is 200

 {

"_id": "11",

"cardholders": {

"100": [

{

"message": "message1",

"time": "timestamp"

},

{

"message": "message2",

"time": "timestamp"

}

],

"200": [

{

"message": "message1",

"time": "timestamp"

},

{

"message": "message2",

"time": "timestamp"

},

{

"message": "message3",

"time": "timestamp"

}

],

"300": [

{

"message": "message1",

"time": "timestamp"

},

{

"message": "message2",

"time": "timestamp"

}

]

}

}

Please advice. I have following

 db.test3.find({"message1":{$eq:true}})

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

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

> db.test3.find({cardholders:{$eq:100}})

网友答案:

You essentially want to include a projection argument in your find() query which matches documents containing only the projection field and the _id field. In this case since "200" is the key, you can project it and use the map() cursor to get the values as follows:

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

Output:

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

UPDATE

To make querying easier, I would recommend changing your schema to change the cardholders key into an array that holds embedded documents. These embedded documents would have a key and value fields; the key field holds the previous dynamic keys and the value field holds the array values:

{
  "_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"
            }
        ]
    }
  ]
}

You can then query the embedded documents using a combination of the dot notation and the $elemMatch projection as follows:

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

I think it's not possible to search for a key.

EDIT: Use the $exists Operator on cardholders.(value) e.g.

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

OLD: Maybe change things to:

 {
  "_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)
    }
  ]
}

then index cardholders.value with index type = 1 and use it with the following:

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

(Note: Please tag your question with your programming langue, if any.)

分享给朋友:
您可能感兴趣的文章:
随机阅读: