基本上我试图找到所有与文本搜索不匹配的文档,然后对文档字段进行一些数学运算以返回总数。这是我尝试过的,但它不起作用。
db.my_collection.aggregate(
[
{ $match: { $text: { $search: "annual one-time One_time monthly quarterly" } } },
{ $project: { _id: 0, score: { $meta: "textScore" } } },
{ $group: { _id: {score: "$score"},
totalAmount: { $sum: { $divide: [ { $add: [ "$amount_in_cents" ] }, 100 ] } },
count: { $sum: 1 }
} }
]);
这是我得到的。
[
{
"_id" : {
"score" : 0.7500000000000000
},
"totalAmount" : 0,
"count" : 1.0000000000000000
},
{
"_id" : {
"score" : 1.0000000000000000
},
"totalAmount" : 0,
"count" : 57.0000000000000000
},
{
"_id" : {
"score" : 1.5000000000000000
},
"totalAmount" : 0,
"count" : 7.0000000000000000
}
]
如何使每个分数分组的totalAmount准确无误?如何才能为那些没有任何搜索字词的搜索获得分数分组?