-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmong.js
133 lines (127 loc) · 2.86 KB
/
mong.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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);
});
});