问题
I'm using passport.js to store my users into my mongodb. A user object looks like this
{
"_id" : ObjectId("545ac4930acf4b5394cbc244"),
"local" : {
"password" : [encrypted password],
"email" : "john@domain.com",
"level" : "super user",
},
"__v" : 0
}
I'm attempting to display all the users who are part of the "super user" group.
I'm finding this difficult as my data sits two levels within the object.
回答1:
Use the dot notation like this:
db.users.find({"local.level" : "super user"})
To return only some fields, the find has an optional projection argument. For the password and email you would do something like:
db.users.find({"local.level" : "super user"}, {"local.password":1, "local.email":1, "_id":0})
Note: the _id is always returned unless marked as 0 in the projection.
来源:https://stackoverflow.com/questions/28060977/query-object-within-an-object