Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aggregate data from MongoDB with Node.js and mongoose
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//Database connection
var uristring = 'mongodb://localhost/test';
var mongoOptions = { };
mongoose.connect(uristring, mongoOptions, function (err, res) {
if (err) {
console.log('Error when connecting to: ' + uristring + '. ' + err);
}
else {
console.log('Successfully connected to: ' + uristring);
}
});
//First let's create the Schemas:
// Record Schema
var Record = new Schema({
amount: { type: Number, required: true },
date: { type: Date, required: true },
description: { type: String }
})
// Account Schema
var Account = new Schema({
name: { type: String, required: true },
currency: { type: String, required: true },
created: { type: Date, default: Date.now },
records: [Record]
});
//Define Models
var AccountModel = mongoose.model('Account', Account);
var RecordModel = mongoose.model('Record', Record);
var ObjectId = require('mongoose').Types.ObjectId;
// Utility functions to insert data:
var insertAccount = function(account, callback) {
var Account = new AccountModel();
Account.name = account.name;
Account.currency = account.currency;
Account.records = [];
Account.save(function(err) {
if (err) {
console.log(err);
return null;
}
return callback(Account._id);
});
}
var insertRecords = function(accountId, record, numberOfRecords, callback) {
AccountModel.findOne({_id:accountId}, function(err, account) {
if (err) {
console.log(err);
return ;
}
for (var i = 0; i < numberOfRecords; i++) {
var Record = new RecordModel();
Record.amount = record.amount;
Record.date = new Date();
Record.is_expense = record.is_expense;
account.records.push(Record);
}
account.save(function(err2) {
if (err2) {
console.log(err2);
}
return callback();
});
});
}
var listAccounts = function() {
var query = AccountModel.find();
query.exec(function(err, accounts) {
if (err) {
console.log(err);
}
console.log(accounts);
});
}
var listAccount = function(id) {
if (id == null) {
return ;
}
var query = AccountModel.find({_id:id});
query.field('_id name currency date');
query.exec(function(err, accounts) {
if (err) {
console.log(err);
}
console.log(accounts);
});
}
var removeAccount = function(accountId) {
var query = AccountModel.findOne({_id:accountId});
query.exec(function(err, account) {
if (err) {
console.log(err);
return ;
}
account.remove();
});
}
//Aggregation function
var getBalance = function(accountId) {
AccountModel.aggregate([
{ $match: {
_id: accountId
}},
{ $unwind: "$records" },
{ $group: {
_id: "$_id",
balance: { $sum: "$records.amount" }
}}
], function (err, result) {
if (err) {
console.log(err);
return;
}
console.log(result);
});
}
//Let's add some data and call the getBalance fucntion
insertAccount({name:"First Account", currency: "Dollar"}, function(accountId) {
if (accountId == null) {
console.log("accountId is null");
return ;
}
insertRecords(accountId, {amount:123.5}, 1000, function() {
getBalance(accountId);
});
});
@akluffy

This comment has been minimized.

Copy link

akluffy commented May 27, 2016

How to get some math vale like the mean, std deviation ??

@duaraghav8

This comment has been minimized.

Copy link

duaraghav8 commented Jun 6, 2016

How would you use the $out operator in combination with this? say I had to take $sum of another field (say 'taxes'), how would I perform that? According to me, we first have to take aggregate with $sum of $records.amount, then use $out to give this result to my next (and last) aggregate function which will call $sum on $taxes. Am I correct in this approach?
Would appreciate any help, thanks!

@etimSyteKraft

This comment has been minimized.

Copy link

etimSyteKraft commented May 10, 2017

Is it possible for you to aggregate data from two collections, given one has a dbref for instance?

@tobiamos

This comment has been minimized.

Copy link

tobiamos commented Feb 6, 2018

how to you pass in optional arguments like cursor and explain?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.