0

I have a mongo collection with the document format below

{
    "_id": {
         "objectId": "test1",
         "source": "testSource"
    },
    "val": {
         "testField": "fieldValueOne"
    }
}

The source value is same for all the documents. I am trying to use $in query as below but it is returning empty set.

db.getCollection('my_collection').find({ "_id": {
    "objectId":{"$in": ["test1","test2","test3"]},
    "source": "testSource"
}})

But if I do following simple $in query it returns all the matching documents.

db.getCollection('my_collection').find({
    "_id.objectId":{"$in": ["test1","test2","test3"]}
}})

But I want to include the source too as in future there can be different source values. How can I achieve the desired query? Thanks in advance.

XCEPTION
  • 1,671
  • 1
  • 18
  • 38
  • So just add `"_id.source": "testSource"`. Why would you think you could not? – Neil Lunn Apr 29 '18 at 06:17
  • If you're still baffled, then `{ "_id.objectId":{"$in": ["test1","test2","test3"]}, "_id.source": "testSource" }` you just use both dotted fields. This is perfectly fine. It's not an array so you don't need `$elemMatch` for the two conditions and you don't just specify the whole object, because that's not actually the "exact" match. Just like the linked answer and [the documentation](https://docs.mongodb.com/manual/tutorial/query-embedded-documents/#specify-and-condition) also says. – Neil Lunn Apr 29 '18 at 06:24

0 Answers0