Skip to content

Instantly share code, notes, and snippets.

@dungvtdev
Created August 1, 2017 07:21
Show Gist options
  • Save dungvtdev/241204887261a950af4686355372c65a to your computer and use it in GitHub Desktop.
Save dungvtdev/241204887261a950af4686355372c65a to your computer and use it in GitHub Desktop.

FIND

Find all

db.collection.find()

Find with condition

db.collection.find({ "date": { $gte:ISODate("2017-07-15T00:00:00Z"), $lt:ISODate("2017-07-16T00:00:00Z") }, "website_id": ObjectId("596f4ea91d41c8acb2d8efdf") })

Find and get specific fields

db.collection.find({}, {"website_id":1})

COUNT

Same as FIND: db.collection.count(...)

AGGREGATION

Group all as multiple groups

db.collection.aggregate( [ { $group: { _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } }, totalAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } }, count: { $sum: 1 } } } ] )

Group all as one group (to use accumulate function ex $sum...)

db.collection.aggregate( ... _id: null, ... )

Group filled items.

db.days.aggregate( [ { $match: { "date":{ $lte: ISODate("2017-07-10T00:00:00Z"), $gte: ISODate("2017-06-30T00:00:00Z") } } }, { $group : { _id: null, count: { $sum: 1} } } ] )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment