1

I have a data structure like this, I want to pull out "userNames" with "isUserFixed" 0 from users array.

{
   "_id" : ObjectId("5b8e7fda725bec7b317b94f6"),
   "email" : "test@test.com",
   "users" : [ 
       {
           "userName" : "user1",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user2",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user3",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user4",
           "isUserFixed" : 0
       }, 
       {
           "userName" : "user5",
           "isUserFixed" : 0
       }
   ]
}

Any help would be appreciated.

1 Answers1

2

Hope this helps:

db.getCollection('').aggregate([{"$match": {"Your match criteria key": "Your match criteria value"}},
    {"$group" : {"_id" : {"email": "$email"}}},        
    {
                "$project": {
                    "users": {
                        "$filter": {
                            "input": "$users",
                            "as": "user",
                            "cond": {
                                "$in": ["$$user.isUserFixed", [0]]
                            }
                        }
                    }
                }
            }

        ])
Ankit
  • 960
  • 6
  • 12