给出以下聚合的匹配($match
):
[
"palabra": "Hello",
"palabra": "My name is Rafael"
]
pipeline
的下一个aggregate
给了我{2}的$sum
总数:
var pipeline = [
{
$match: {
"_p_evaluatedByUser": "_User$" + userId
}
},
{
$project: {
palabra: 1
}
},
{
$unwind: "$palabra"
},
{
$group: {
_id: "result",
count: {
$sum: 1
}
}
}
];
我实际上正在寻找一个pipeline
$sum: $palabra.split(" ").length
能够5
,而不是2
。
我最初的pipeline
是否有任何简单的方法?
答案 0 :(得分:1)
在你的$ unwind之后:
{
$project: {
"words": {$split: ["$palabra", " "]}
}
},
{
$unwind: "$words"
},
{
$group: {
_id: null,
allwords: {$push: "$words"}
}
},
{
$project: {
wordCount: {$size: "$allwords"}
}
}