【发布时间】:2020-12-22 19:08:57
【问题描述】:
我有一个像下面这样的集合
{
"id":"5fd13c33ac0277c435117519",
"content":"test",
"votes":[
{
"user":"22",
"isLike":true
},
{
"user":"25",
"isLike":false
},
{
"user":"43",
"isLike":false
}
]
},
{
"id":"5fd13c33ac0277c435237443",
"content":"test 2",
"votes":[
{
"user":"25",
"isLike":true
},
{
"user":"43",
"isLike":false
}
]
}
如何使用 c# 驱动程序通过查询 votes.user -ie 获得以下结果。 25-然后合并父数组和嵌套数组中的这两个字段?
{
"id":"5fd13c33ac0277c435117519",
"isLike":false
},
{
"id":"5fd13c33ac0277c435237443",
"isLike":true
}
编辑:在 mongoplayground.net 上尝试并失败后,我得到了结果,但仍不确定如何通过 c# 驱动程序将其转换。
db.collection.aggregate([
{
$unwind: "$votes"
},
{
$match: {
"votes.user": "25"
}
},
{
$replaceWith: {
id: "$id",
isLike: "$votes.isLike"
}
}
])
【问题讨论】:
标签: c# mongodb aggregation-framework mongodb-.net-driver