node.jsmongodbmongoose

filtering document in mongoose based on id and one specific property


just stuck on how to filter the below document on the basis of user_id and popupsArray.isSeen = true.

enter image description here

I want to get the popups from popupsArray whose isSeen = FALSE


Solution

  • You can simply use the $filter in aggregation

    db.collection.aggregate([
      {
        $set: {
          popupsArray: {
            "$filter": {
              "input": "$popupsArray",
              "cond": {
                $eq: [
                  "$$this.isSeen",
                  false
                ]
              }
            }
          }
        }
      }
    ])
    

    Working Mongo playground