给定这样的集合:..
[
{
"_id" : ObjectId("5546329a470000850084a621"),
"name": "Joe",
"surname": "Smith",
"accounts": [
{
"_id" : ObjectId("5546329a470000850084a655"),
"default": true,
"status" : "approved",
"activationTime" : ISODate("2013-05-03T14:37:15.025Z")
},
{
"_id" : ObjectId("5546329a470000850084a688"),
"default": true,
"status" : "approved",
"activationTime" : ISODate("2014-06-03T14:37:15.025Z")
}
]
},
{
"_id" : ObjectId("9546329a470079850084a622"),
"name": "Jimmy",
"surname": "Brown",
"accounts": [
{
"_id" : ObjectId("5546329a470790850084a651"),
"default": true,
"status" : "suspended",
"activationTime" : ISODate("2015-02-03T14:37:15.025Z")
},
{
"_id" : ObjectId("5546329a470019850084a611"),
"default": true,
"status" : "approved",
"activationTime" : ISODate("2015-04-03T14:37:15.025Z")
}
]
},
]
...我如何找到文件accounts.N._id
?我试过这个...
db.users.find(
{},
{
"accounts": 0, "accounts": {
"$elemMatch": { "_id" : ObjectId("5546329a470019850084a611"), "default": true }
}
}
)
...但它不起作用,因为我只得到_id
所有文件中的一个:
{ "_id" : ObjectId("5546329a470000850084a621") }
{ "_id" : ObjectId("9546329a470079850084a622") }
我错过了什么吗?
编辑
我真正需要的结果是这样的:
{
"_id" : ObjectId("9546329a470079850084a622"),
"name": "Jimmy",
"surname": "Brown"
}
例如,我需要查找accounts.N._id
但不显示嵌套文档本身。