我在想这是否可能,还是我需要使用聚合管道?
我已阅读过像this这样的帖子,并直觉地感觉这是可能的。
文档示例:
我正在尝试使用投影字段中的点符号通过索引位置访问“评论”中的特定评论,具体方法如下:
如果我去掉
如果我去掉
我已阅读过像this这样的帖子,并直觉地感觉这是可能的。
文档示例:
{
"_id": ObjectID("5143ddf3bcf1bfab37d9c6f"),
"permalink": "btxcacmbqkxbpgtpeero",
"author": "machine",
"title": "Declaration of Independence",
"tags": [
"study",
"law"
],
"comments": [
{
"body": "comment 1",
"email": "email_1@test.com",
"author": "machine_1"
},
{
"body": "comment 2",
"email": "email_2@test.com",
"author": "machine_2"
},
{
"body": "comment 3",
"email": "email_3@test.com",
"author": "machine_3"
},
]
"date": ISODate("2013-03-16T02:50:27.878Z")
}
我正在尝试使用投影字段中的点符号通过索引位置访问“评论”中的特定评论,具体方法如下:
db.collection.find({permalink: "btxcacmbqkxbpgtpeero"}, {'comments.0.1.': 1})
其中comments.0
是字段中的第一项:数组,而.1
是数组中的第二个评论。
我得到的结果:
{ "_id" : ObjectID("5143ddf3bcf1bfab37d9c6f"), "comments" : [ { }, { }, { } ] }
如果我去掉
.1
,只留下comments.0
,我得到相同的结果:{ "_id" : ObjectID("5143ddf3bcf1bfab37d9c6f"), "comments" : [ { }, { }, { } ] }
如果我去掉
.0
,只留下comments
,那么我会得到仍然在它们的数组中的评论:[
{
"body": "comment 1",
"email": "email_1@test.com",
"author": "machine_1"
},
{
"body": "comment 2",
"email": "email_2@test.com",
"author": "machine_2"
},
{
"body": "comment 3",
"email": "email_3@test.com",
"author": "machine_3"
}
]
这可以做到吗?如果可以,怎么做?