4

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?

1
  • Ever heard of "embedded documents"? Commented Aug 31, 2012 at 17:10

2 Answers 2

9

You have to use dot notation to match inside of embedded array objects:

db.coll.find({name: 'montalto', 'users.username': 'ciccio'}).count()
Sign up to request clarification or add additional context in comments.

2 Comments

my code var cursor = db.collection('user-data').find({ "title": title, "author": author }).count();...i just console this cursor variable..but it prints [object promise].doesn't return the count of matched users..any suggestions?
@Jana Go ahead and post that as a new question.
6

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.