I wonder if there is a way to get a single object instead of an array when querying for a single embedded document in MongoDB
I have Groups with embedded Users
{
groupname: "Admins",
users: [
{
email: bob@google.com,
first_name: 'bob'
},
{...},
{...} // multiple embedded users
]
}
I can query a single user from a group with this query
db.groups.find({'users.email' => bob@google.com}, {'users.$' => 1})
but it gives me a 'users' array with 1 user init
{
groupname: "Admins",
users: [
{
email: bob@google.com,
first_name: 'bob'
}
]
}
then I have to select the first element in the array,
users[0]
there is no problem with it, but then i just have to write more code in my application, the better way should be
user (-s)
so I can query
user.first_name
if someone knows a way let me know