2

I have a dynamic key-value object in my MongoDB.

For eg:

{
    "cs": {
        "fromId": 42,
        "toId": 39,
        "com": "testing dilip",
        "ts": 1528199910109,
        "isapproved": false
    },
    "custInst": {
      "21705": [ 1, 2 ],
      "22032": [ 1 ],
      "22389": [ 1, 2, 4 ]
    },
    "id" : 6
}

Now I Want to query in custInt and want to return all the documents that exist in that collection where custInt contains key 21705 .

Update

I want to know how to query on keys if I have an array and I want to perform sorf of like $in operation.

For eg:

I want to query and return documents which contain custInst [21705,22032].

How do I query this in MongoDB?

Any help will be helpful.

Thanks.

Saif Ali Khan
  • 818
  • 3
  • 9
  • 34

1 Answers1

4

Try mongodb's $exists operator:

db.example.find({"custInst.21705": {"$exists": true}})

More information

Tamas Szoke
  • 5,426
  • 4
  • 24
  • 39