0

I have a DB as “link” and a collection within this DB as “products”. The “products” collection is having below records -

MongoDBCollectionStructure

Now I have an input array with below 3 ‘productconfig._id’ – 5984b2f0ca0a093c74cb4395, 598d57c8b541eb3e58e98cf7, 5984b48eca0a093c74cb4397 Now based on that, I want my output like this –

MongoDBExpectedResult

(Note that I don’t want the 3rd object of productconfig Array from document 1)

I had tried below 2 queries -

  1. db.products.find({"productconfig._id":{$in: [ObjectId("5984b2f0ca0a093c74cb4395"), ObjectId("598d57c8b541eb3e58e98cf7"), ObjectId("5984b48eca0a093c74cb4397")]}},{"name":1, "productconfig.$":1}).pretty()

  2. 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

rishidwdi
  • 23
  • 4
  • The [positional `$` operator](https://docs.mongodb.com/manual/reference/operator/projection/positional/) only returns the **first** match just as documented. If you want "multiple matches" then you need to use `$filter` with aggregate or one of the other listed approaches if your MongoDB is actually quite old and does not support that. But these are actually all demonstrated already – Neil Lunn Aug 25 '17 at 05:37
  • Hi @Neil Lunn, I am relatively new to MongoDB. Can you please give me the exact query using $filter as you mentioned. – rishidwdi Aug 25 '17 at 05:47
  • You need to show some kind of effort here. You were pointed to existing answers as a point of "learning". The point of marking as a duplicate is there is little point in having multiple questions around with essentially the same answer. So statements like *"unable to apply"* actually read a lot more like *"have not even tried"* when there is no actual attempt shown at applying what you should have learned from existing answers and linked documentation therein. – Neil Lunn Aug 25 '17 at 08:01
  • Hi @Neil Lunn, I had tried using below sql - db.products.aggregate([ {$match:{'productconfig._id':myArray}}, {$project: { productconfig: {$filter: { input: '$productconfig', as: 'productconf', cond: {$eq: ['$$productconf._id',myArray]} } } }} ]) Where myArray is an array with three values. This is not giving me any result. :( – rishidwdi Aug 25 '17 at 08:22
  • Keep your additional information In the question itself please. Comments do not format well and are difficult to read. So `myArray` is an array I would presume. Does the `$eq` aggregation operator say that it matches against arrays? Is there possibly something in the [documented operators](https://docs.mongodb.com/manual/reference/operator/aggregation/) that actually does this? Maybe you should be looking there and reading what things actually do. – Neil Lunn Aug 25 '17 at 08:32
  • Hi Neil Lunn, I will make sure the thing you have highlighted next time. On the query side, It doesn't display any error. But not fetching any result either. – rishidwdi Aug 25 '17 at 11:14

0 Answers0