forked from jamesmart77/chedah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
82 lines (69 loc) · 2.32 KB
/
test.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
const db = require("./models");
const plaid = require("plaid");
const R = require("ramda");
require("dotenv").config();
const mongoose = require("mongoose");
// Set up promises with mongoose
mongoose.Promise = global.Promise;
// Connect to the Mongo DB
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/chedah", {
useMongoClient: true
});
const aggregateAccountCategories = db.Transaction.aggregate([
{ $match: { account_id: "RmKbP4d6PWudGwXl91AxCPjEvgEEqpfMvzXeJ" } },
{ $group: { _id: "$category", total: { $sum: "$amount" } } }
]);
const aggregateAccountVendors = db.Transaction.aggregate([
{ $match: { account_id: "RmKbP4d6PWudGwXl91AxCPjEvgEEqpfMvzXeJ" } },
{ $group: { _id: "$transactionName", total: { $sum: "$amount" } } }
]);
const aggregateGigCategories = db.Transaction.aggregate([
{ $match: { gigId: '5aa1bb8b6b7126f2f5314126' } },
{ $group: { _id: "$category", total: { $sum: "$amount" } } }
]);
const aggregateGigVendors = db.Transaction.aggregate([
{ $match: { gigId: '5aa1bb8b6b7126f2f5314126' } },
{ $group: { _id: "$transactionName", total: { $sum: "$amount" } } }
]);
// const user = db.User.findOne({ auth_id: req.params.authId }).lean()
const user = db.User.findOne({ auth_id: "facebook|10155456253012712" })
.lean()
.populate("accounts")
.populate("transactions")
.populate("gigs")
.populate("categories")
.populate({
path: "gigs",
populate: {
path: "goals",
model: "Goal"
}
})
const gigs = db.Gig.find().lean()
const accounts = db.Account.find().lean()
const categories = db.Category.find().lean();
Promise.all([
user,
gigs,
accounts
// categories,
// aggregateAccountCategories,
// aggregateAccountVendors,
// aggregateGigCategories,
// aggregateGigVendors
])
.then(([user, gigs, accounts]) => {
user.transactions.map(t => t.gigName = gigs.find(gig => t.gigId === gig._id.toString()).name)
user.transactions.map(t => t.accountName = accounts.find(account => t.gigId === account.account_id.toString()).name)
console.log(user.transactions)
})
.catch(console.log);
// {
// $lookup:
// {
// from: <collection to join>,
// localField: <field from the input documents>,
// foreignField: <field from the documents of the "from" collection>,
// as: <output array field>
// }
// }