Arrays mongodb将单个嵌入文档作为对象而不是数组查找

Arrays mongodb将单个嵌入文档作为对象而不是数组查找,arrays,mongodb,object,embedded-documents,Arrays,Mongodb,Object,Embedded Documents,我想知道在MongoDB中查询单个嵌入文档时,是否有方法获取单个对象而不是数组 我有群和嵌入式用户 { groupname: "Admins", users: [ { email: bob@google.com, first_name: 'bob' }, {...}, {...} // multiple embedded users ] } 我可以使用此查询从组中查询单个用户 db.groups.find({'users

我想知道在MongoDB中查询单个嵌入文档时,是否有方法获取单个对象而不是数组

我有和嵌入式用户

{
 groupname: "Admins",
 users: [
    { 
        email: bob@google.com, 
        first_name: 'bob'
    },
    {...},
    {...} // multiple embedded users
 ]
}
我可以使用此查询从组中查询单个用户

db.groups.find({'users.email' => bob@google.com}, {'users.$' => 1})
但是它给了我一个带有1个用户初始化的“users”数组

{
 groupname: "Admins",
 users: [
    { 
        email: bob@google.com, 
        first_name: 'bob'
    }
 ]
}
然后我必须选择数组中的第一个元素

users[0] 
它没有问题,但是我只需要在我的应用程序中编写更多的代码,更好的方法应该是

user (-s)
所以我可以查询

user.first_name

如果有人知道一种方法,请让我知道

根据您正在使用的驱动程序,findOne已被弃用,您应该使用find().limit(1).next(function(err,doc){})

You can use findOne as it returns a single document, where find returns a cursor.

>user =  db.groups.findOne({'users.email' : bob@google.com}, {'users.$' => 1})
>user.first_name