【发布时间】:2019-03-19 13:55:16
【问题描述】:
DB如下。
收藏:评论
{
"_id" : ObjectId("5bc4348f8e798ccb030991e8")
"comment_no" : 143,
“comment_content” : “test test”
}
收藏:发布
{
"_id" : ObjectId("5bc16b068e798ccb03096efa"),
"post_no" : 48,
"comment" : [
{
"comment_no" : 143,
"comment_group" : 1 // This value will disappear.
}
]
}
查询如下所示:
db.getCollection('post').aggregate([
{
$match : {post_no: 48}
},
{
$lookup: {
from: 'comment'
localField: 'comment.comment_no',
foreignField: 'comment_no',
as: 'comment'
}
}
])
结果如下。
{
"_id" : ObjectId("5bc16b068e798ccb03096efa"),
"post_no" : 48,
"comment" : [
{
"comment_no" : 143,
“comment_content” : “test test”
}
]
}
现有数据消失,但我想合并comment_group的值。
例如,我想要的结果是..
{
"_id" : ObjectId("5bc16b068e798ccb03096efa"),
"post_no" : 48,
"comment" : [
{
"comment_no" : 143,
“comment_content” : “test test”,
"comment_group" : 1 // Here!! I want to use this value.
}
]
}
我可以进行查询,以便将值合并到 comment_no 中吗?
【问题讨论】:
标签: mongodb aggregation-framework lookup