Tutorial by Examples

How do you get the number of Debit and Credit transactions? One way to do it is by using count() function as below. > db.transactions.count({cr_dr : "D"}); or > db.transactions.find({cr_dr : "D"}).length(); But what if you do not know the possible values of cr_dr up...

Sum

How to get the summation of amount? See the below aggregate query. > db.transactions.aggregate( [ { $group : { _id : '$cr_dr', count : {$sum : 1}, //counts the number totalAmount : {$sum : '$amount'} //sum...
How to get the average amount of debit and credit transactions? > db.transactions.aggregate( [ { $group : { _id : '$cr_dr', // group by type of transaction (debit or credit) count : {$sum : 1}, // number of transaction for each type...
When you want to work with the data entries in arrays you first need to unwind the array. The unwind operation creates a document for each entry in the array. When you have lot's of documents with large arrays you will see an explosion in number of documents. { "_id" : 1, "item"...
How to write a query to get all departments where average age of employees making less than or $70000 is greather than or equal to 35? In order to that we need to write a query to match employees that have a salary that is less than or equal to $70000. Then add the aggregate stage to group the empl...
Note that the allowDiskUse: true option is optional but will help mitigate out of memory issues as this aggregation can be a memory intensive operation if your collection size is large - so i recommend to always use it. var duplicates = []; db.transactions.aggregate([ { $group: { _id: { c...

Page 1 of 1