0

So I'm kinda new to node.js. And I can't understand why Aggregate queries don't work for me. I've tried the same query at the Mongo Shell and it worked perfectly. Here's the code:

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);
});

Console output:

undefined
undefined
undefined

1 Answer 1

1

Aggregation argument needs to be an array, because aggregation works on pipeline concept where output of last pipeline will become input for current pipeline.

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.