Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  77] [ 7]  / answers: 1 / hits: 51041  / 10 Years ago, thu, october 16, 2014, 12:00:00

I can get all stats of the site with aggregation but I want to it for a certain user, like $where.



All stats:



games.aggregate([{
$group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}]).exec(function ( e, d ) {

console.log( d )

})


When I try to use $match operator, I'm getting error :



games.aggregate([{
$match: { '$game_user_id' : '12345789' },
$group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}]).exec(function ( e, d ) {

console.log( d )

})

Arguments must be aggregate pipeline operators


What am I missing?


More From » node.js

 Answers
4

Pipeline stages are separate BSON documents in the array:



games.aggregate([
{ $match: { 'game_user_id' : '12345789' } },
{ $group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}}
]).exec(function ( e, d ) {
console.log( d )
});


So the Array or [] bracket notation in JavaScript means it expects a list to be provided. This means a list of documents which are generally specified in JSON notation with {} braces.


[#69106] Monday, October 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
lucianod questions
;