Michael
Michael

Reputation: 16142

MongoDB aggregate specify criteria?

I am using mongodb aggregate to get already filtered data in response on my node server.

Some.aggregate([
    { $group: {
        _id: '$model',
        someAvg: { $avg: '$some' },
        someAvg2: { $avg: '$some2' },
        someSum3: { $sum: '$some3' }
    }}
], function (err, results) {
    ...

But all this does is returns the Models without duplicates and averages some, some2, some3 of these models. When you are using find, you can specify something like:

Some.find({model:"Thinkpad T430"}, function(...

So it returns only data sells with model value of "Thinkpad T430".

Is there a way to do the similar thing with aggregate?

Upvotes: 0

Views: 35

Answers (1)

krl
krl

Reputation: 5296

Some.aggregate([
    { $match : { 
        model:"Thinkpad T430"
    }},
    { $group: {
        _id: '$model',
        someAvg: { $avg: '$some' },
        someAvg2: { $avg: '$some2' },
        someSum3: { $sum: '$some3' }
    }}
], function (err, results) {
    ...

Upvotes: 1

Related Questions