Assuming that the mongo structure is as follows
> db.conversation.find().pretty()
{
"_id" : ObjectId("55337db16f20d74ec08c33b1"),
"title" : "title",
"users" : [
{
"name" : "a",
"enabled" : true
},
{
"name" : "b",
"enabled" : false
}
]
}
{
"_id" : ObjectId("553380ac6f20d74ec08c33b2"),
"users" : [
{
"name" : "a",
"enabled" : false
},
{
"name" : "b",
"enabled" : false
}
],
"title" : "Abc"
}
If you wish to return those entries that have users with "enabled" field set to "true", then following would do
> db.conversation.find({users: {$elemMatch: { "name":{$exists: true} ,"enabled":true} }}).pretty()
{
"_id" : ObjectId("55337db16f20d74ec08c33b1"),
"title" : "title",
"users" : [
{
"name" : "a",
"enabled" : true
},
{
"name" : "b",
"enabled" : false
}
]
}
But I don't like the data structure you are using.
Let's say (assuming that would be case) you have to keep track of a conversation. Will the following work for you?
conversation = {title: "title", users: ["a", "b"]}
Assuming in your case that "a","b" are unique identifications, otherwise I'd recommend you keep the IDs here.
conversation = {title: "title", users: [3434, 984]}
Have another collection that stores your users data(following would be typical entry).
{"user_id": 3434, "enabled": true}
...
In this case, you can run an aggregation or something, selecting user list from conversations collection to return you the unique list of all users. You can then query that list in your users collection to see which are actually enabled/disabled.
Now, assuming I am wrong and this enabled disabled is actually indicating which user is enabled/disabled in a particular conversation.
Then I'd say your structure should be.
conversation = {title: "title", users: [232, 3453], disabled: [232]}
Hope this helps!