0

I'm looking for a solution to filter a mongodb aggregation using the $in operator.

It looks for an id string inside an array of strings using an aggregate query, quite simple in concept:

db.post.aggregate({ $project: {  
  present: { $in: ["569d659d1b5075f9020041be", "$comments" ] }, 
  name: "$name"  } 
})

This query gives me the projection I need, in the shape:

{ 
  "_id" : ObjectId("545d6a491b5075ec020041d8"), 
  "present" : false, 
  "name" : "Clarity Of Vision" 
}

How can I filter that projection in order to get, for example the projected elements in which present is true?

Is there a way to do it in the same aggregate query?

Evhz
  • 8,852
  • 9
  • 51
  • 69
  • @Neil Lunn we definetively need to change this. As the question is not a dupplicate, even if it has an answer already in other question that didn't specifically asked for that. So under my point of view, to have the title present as a SEO linker, helps others to find the answer. – Evhz May 02 '18 at 10:56
  • The question could not be more of a duplicate. Ironic as to the title `filter` in the question is in fact the exact operator for what you want to `$filter`. Takes you more than 2 minutes to actually read and understand answers, so I'm always less than impressed with such responses in such a short timeframe. You get the matching entries with `$filter`, and you mark each entry using `$map`. All those methods are already listed in existing answers. – Neil Lunn May 02 '18 at 11:01
  • And guess what happens from SEO terms to questions marked as duplicates. Yes those titles will also come under consideration, and of course they link to where the answers are. – Neil Lunn May 02 '18 at 11:02

0 Answers0