I've this collections
{
"name" : "montalto",
"users" : [
{
"username" : "ciccio",
"email" : "aaaaaaaa",
"password" : "aaaaaaaa",
"money" : 0
}
],
"numers" : "8",
"_id" : ObjectId("5040d3fded299bf03a000002")
}
If I want to search for a collection with the name of montalto and a user named ciccio I'm using the following query:
db.coll.find({name:'montalto', users:{username:'ciccio'}}).count()
But it does not work. Where I went wrong?
You have to use dot notation to match inside of embedded array objects:
db.coll.find({name: 'montalto', 'users.username': 'ciccio'}).count()
To clarify a collection is a group of documents. So your terminology in this question is slightly off.
To access embedded documents or objects you simply use dot notation as you would in traditional javascript.
db.coll.find({name: 'montalto', 'users.username': 'ciccio'})
You can of course add the .count()
on the end of that.