Query object within an object

烂漫一生 提交于 2019-12-12 03:26:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!