Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

109
Views
Not able to filter out the array of object in mongodb
{
    "_id" : ObjectId("61dbab7f0beb8c8075370217"),
    "name" : "Question 2",
    "answers" : [
        {
            "text" : "something 1",
            "isDeleted" : false
        },
        {
            "text" : "something 2",
            "isDeleted" : true
        },
        {
            "text" : "something 2",
            "isDeleted" : false
        }
    ]
}

I have this kind of document where I want to find all answers objects which have isDeleted is false.

9 months ago · Santiago Trujillo
1 answers
Answer question

0

You can use $filter

db.collection.find({},
{
  answers: {
    $filter: {
      input: "$answers",
      cond: {
        $eq: [
          "$$this.isDeleted",
          false
        ]
      }
    }
  },
  name :1
})

Working Mongo playground

9 months ago · Santiago Trujillo Report
Answer question
Find remote jobs