假设有这样一个集合:
[
{
"_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
找到一个文档呢?我试过了以下方法...
...
=>...
accounts.N._id
andaccounts.N._id
=> 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
查找,但不显示嵌套文档本身。