-1

I need to form a query in such a way to get only status : active list. It should list only the active status. need to filter it down .

"users" : {
                "assigned" : [
                        {
                                "userId" : ObjectId("5d9fd84b3d598088d2ea5e0b"),
                                "firstName" : "ramasamy",
                                "userImage" : "69_20160719103303_user.png",
                                "status" : "inactive",
                                "assignedDate" : ISODate("2017-05-25T05:18:37Z")
                        },
                        {
                                "userId" : ObjectId("5d9fd8233d598088d2ea5dd1"),
                                "firstName" : "karthik",
                                "userImage" : "karthik_1589782232510.jpeg",
                                "status" : "active",
                                "assignedDate" : null
                        }],
                        "incharge" : [{
                                "userId" : ObjectId("5d9fd81f3d598088d2ea5dcc"),
                                "firstName" : "sathish",
                                 "status" : "inactive",
                                "assignedDate" : ISODate("2017-04-12T16:43:24Z")
                        },
                        { "userId" : ObjectId("5d9fd8203d598088d2ea5dcd"),
                                "firstName" : "santhana vadivelan",
                                "status" : "active",
                                "assignedDate" : ISODate("2017-05-09T18:20:25Z")
                        }]
                }

I tried using find query,but noting worked out.

I need the result to be like.

"users" : {
                "assigned" : [
                        {
                                "userId" : ObjectId("5d9fd8233d598088d2ea5dd1"),
                                "firstName" : "karthik",
                                "userImage" : "karthik_1589782232510.jpeg",
                                "status" : "active",
                                "assignedDate" : null
                        }]
                        "incharge" : [{ "userId" : ObjectId("5d9fd8203d598088d2ea5dcd"),
                                "firstName" : "santhana vadivelan",
                                "status" : "active",
                                "assignedDate" : ISODate("2017-05-09T18:20:25Z")
                        }]
                }

1 Answer 1

3

You can use the $ keyword. This will allow you to perform queries on all the elements in an inner array.

For example: db.collection.find({'users.assigned.$.status': 'active'})

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.