I have a DB as “link” and a collection within this DB as “products”. The “products” collection is having below records -
Now I have an input array with below 3 ‘productconfig._id’ – 5984b2f0ca0a093c74cb4395, 598d57c8b541eb3e58e98cf7, 5984b48eca0a093c74cb4397 Now based on that, I want my output like this –
(Note that I don’t want the 3rd object of productconfig Array from document 1)
I had tried below 2 queries -
db.products.find({"productconfig._id":{$in: [ObjectId("5984b2f0ca0a093c74cb4395"), ObjectId("598d57c8b541eb3e58e98cf7"), ObjectId("5984b48eca0a093c74cb4397")]}},{"name":1, "productconfig.$":1}).pretty()
db.products.find({"productconfig":{$elemMatch: {"_id": {$in: [ObjectId("5984b2f0ca0a093c74cb4395"), ObjectId("598d57c8b541eb3e58e98cf7"), ObjectId("5984b48eca0a093c74cb4397")]}}}},{"name":1, "productconfig.$":1}).pretty()
But these queries are not returning the currect output as the output is not having productconfig._id=598d57c8b541eb3e58e98cf7 record.
Please help.
I know there is a similar question asked in in below link - Retrieve only the queried element in an object array in MongoDB collection
However I am not able to apply that $filter logic in here. Appreciate if someone can please let me know the query