![](/img/trans.png)
[英]How to paginate aggregation query results in MongoDB and get a total document count (Node.js + Mongoose)?
[英]Node.js: Query Aggregation not working (mongodb)
因此,我是node.js的新手。 而且我不明白為什么匯總查詢對我不起作用。 我已經在Mongo Shell上嘗試了相同的查詢,並且效果很好。 這是代碼:
db.collection("companies").aggregate({$match:
{$or:
[{ $and:
[{ left: {$gt: 2}}, {left: {$lt: 11}}]},
{ $and:
[{ right: {$gt: 2}}, {right: {$lt: 11}}]}]}},
{ $group:
{_id:null,
Sum:{$sum: "$earn"}}},
function(err, data){
if (err) throw err;
console.log(data[0]);
console.log(data.value);
console.log(data.Sum);
});
控制台輸出:
undefined
undefined
undefined
聚合參數必須是一個數組,因為聚合適用於管道概念,其中最后一個管道的輸出將成為當前管道的輸入。
db.collection("companies").aggregate([
{$match:
{$or:
[{ $and:
[{ left: {$gt: 2}}, {left: {$lt: 11}}]},
{ $and:
[{ right: {$gt: 2}}, {right: {$lt: 11}}]}]
}},
{ $group:
{_id:null,
Sum:{$sum: "$earn"}}}],
function(err, data){
if (err) throw err;
console.log(data[0]);
console.log(data.value);
console.log(data.Sum);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.