查询对象内的对象 [英] Query object within an object

查看:52
本文介绍了查询对象内的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用passport.js 将我的用户存储到我的mongodb 中.一个用户对象看起来像这样

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.

推荐答案

像这样使用点符号:

db.users.find({"local.level" : "super user"})

为了只返回一些字段,find 有一个可选的投影参数.对于密码和电子邮件,您可以执行以下操作:

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})

注意:除非在投影中标记为 0,否则始终返回 _id.

Note: the _id is always returned unless marked as 0 in the projection.

这篇关于查询对象内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆