-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
126 lines (114 loc) · 3.28 KB
/
mongo.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
const mongoose = require("mongoose");
async function createCon() {
await mongoose.connect(process.env.MONGO_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
bufferCommands: false,
// bufferMaxEntries: 0,
});
}
const Objecc =
mongoose.models.datas ||
mongoose.model("datas", {
"Product Form: Name": String,
"Product Form: Last Name": String,
"Email": String,
"Product Form: Mobile Number": String,
"Product Form: University": String,
"Product Form: Are you interested in a coach service from any of the following locations?": String,
"Product Form: Graduation Year": String,
"Lineitem variant": String, // Shuttle
"Total": String, // Price paid
"Product Form: Any Dietary Requirements?": String,
"Product Form: Any Allergies?": String,
});
exports.mongoAdd = async (data) => {
await createCon();
try {
await mongoose.connection.db.dropCollection("datas");
} catch (e) {
console.log("Unable to delete collection 'datas'");
console.log(e);
}
const promises = [];
for (let i = 0; i < data.length; i++) {
const obj = new Objecc(data[i]);
promises.push(obj.save());
}
await Promise.allSettled(promises);
};
exports.distinct = async (option) => {
await createCon();
const data = await Objecc.distinct(option);
return data;
};
exports.countUnis = async (unis) => {
await createCon();
output = [];
for (let i = 0; i < unis.length; i++) {
const uni = unis[i];
const count = await Objecc.countDocuments({
"Product Form: University": uni,
});
output.push({ uni, count });
}
return output;
};
exports.countCoaches = async (coaches) => {
await createCon();
output = [];
for (let i = 0; i < coaches.length; i++) {
const coach = coaches[i];
const count = await Objecc.countDocuments({
"Product Form: Are you interested in a coach service from any of the following locations?": coach,
});
output.push({ coach, count });
}
return output;
};
exports.count = async () => {
await createCon();
const count = await Objecc.countDocuments();
return count;
};
exports.list = async (uni = { $regex: ".*" }) => {
await createCon();
const data = await Objecc.find(
{
"Product Form: University": uni,
},
{
"Product Form: Name": 1,
"Product Form: Last Name": 1,
"Product Form: Mobile Number": 1,
"Email": 1,
"Product Form: University": 1,
"Product Form: Are you interested in a coach service from any of the following locations?": 1,
"Product Form: Graduation Year": 1,
"Lineitem variant": 1, // Shuttle
"Total": 1, // Price paid
"Product Form: Any Dietary Requirements?": 1,
"Product Form: Any Allergies?": 1,
_id: 0,
}
);
return data;
};
exports.listByCoach = async (uni = { $regex: ".*" }) => {
await createCon();
const data = await Objecc.find(
{
"Product Form: Are you interested in a coach service from any of the following locations?": uni,
},
{
"Product Form: Name": 1,
"Product Form: Last Name": 1,
"Product Form: Mobile Number": 1,
"Email": 1,
"Product Form: University": 1,
"Product Form: Are you interested in a coach service from any of the following locations?": 1,
_id: 0,
}
);
return data;
};