我正在尝试执行一个棘手的聚合以返回集合中文档内嵌套数组的大小。
以下是重新创建示例数据的方法:
db.test.insert({
projects: [
{
_id: 1,
comments: [
'a',
'b',
'c'
]
},
{
_id: 2,
comments: [
'a',
'b'
]
},
{
_id: 3,
comments: []
}
]
})
Run Code Online (Sandbox Code Playgroud)
我要执行的聚合在这里:
db.test.aggregate([
// enter aggregation here
])
Run Code Online (Sandbox Code Playgroud)
这是所需的输出:
[{
projects: [
{
_id: 1,
comment_count: 3
},
{
_id: 2,
comment_count: 2
},
{
_id: 3,
comment_count: 0
}
]
}]
Run Code Online (Sandbox Code Playgroud)
我正在为如何写这个而苦苦挣扎。如果我尝试以下操作:
"projects.comment_count": {"$size": }
结果返回结果数组的大小:
[{
projects: [
{
_id: 1,
comment_count: 3
},
{
_id: 2, …Run Code Online (Sandbox Code Playgroud)