Skip to content

Commit c4765ad

Browse files
mongo aggregate/group/sum/avg
1 parent c40a7b6 commit c4765ad

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: mongoAggregate.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var mongo=require('mongodb').MongoClient;
2+
mongo.connect('mongodb://localhost:27017/learnyoumongo',function (er,db) {
3+
if (er) console.log('Eroare:'+er);
4+
var col=db.collection('prices');
5+
//console.log(process.argv[2]);
6+
//col.find().toArray(function (err,dat){ console.log(dat)});//display all records
7+
col.aggregate(
8+
[{$match: {size:process.argv[2]}},
9+
{$group : {
10+
_id:'total',
11+
total: {$avg : '$price'}
12+
}
13+
}]).toArray(function(err,results)
14+
{
15+
if (err) throw err;
16+
console.log(Number(results[0].total).toFixed(2));
17+
db.close();
18+
});
19+
});

0 commit comments

Comments
 (0)