This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
137 lines (134 loc) · 3.23 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
127
128
129
130
131
132
133
134
135
136
137
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var url =
'mongodb://chatomation:[email protected]:25255/heroku_9zhjv4th';
var dbName = 'heroku_9zhjv4th';
var collectionName = 'chatomation-users';
var liveHappyCollectionName = 'live-happy-users';
var monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
var self = this;
self.insertNewUserDocument = function(userObject, callback) {
MongoClient.connect(
url,
{ useNewUrlParser: true },
function(err, db) {
if (err) throw err;
var dbo = db.db(dbName);
var collection = dbo.collection(collectionName);
collection.insert(userObject, function(err, result) {
if (err) throw err;
callback(result);
db.close();
});
}
);
};
self.getEmail = function(queryObject, callback) {
MongoClient.connect(
url,
{ useNewUrlParser: true },
function(err, db) {
if (err) throw err;
var dbo = db.db(dbName);
dbo
.collection(collectionName)
.find(queryObject)
.toArray(function(error, docs) {
if (error) throw error;
//random doc
var doc = docs[Math.floor(Math.random() * docs.length)];
callback(doc);
db.close();
});
}
);
};
self.updateDocument = function(queryObject, updateObject, callback) {
MongoClient.connect(
url,
{ useNewUrlParser: true },
function(err, db) {
if (err) throw err;
var dbo = db.db(dbName);
var collection = dbo.collection(collectionName);
collection.updateOne({ queryObject }, { $set: updateObject }, function(
err,
result
) {
if (err) throw err;
callback(result);
db.close();
});
}
);
};
self.liveHappyUsersCount = function(callback) {
MongoClient.connect(
url,
{ useNewUrlParser: true },
function(err, db) {
if (err) throw err;
var dbo = db.db(dbName);
dbo
.collection(liveHappyCollectionName)
.count({}, function(error, numOfDocs) {
if (error) throw error;
callback(numOfDocs);
db.close();
});
}
);
};
self.liveHappyUsersCountByMonth = function(callback) {
MongoClient.connect(
url,
{ useNewUrlParser: true },
function(err, db) {
if (err) throw err;
var dbo = db.db(dbName);
dbo
.collection(liveHappyCollectionName)
.find({}, { projection: { _id: 1 } })
.toArray(function(error, result) {
if (error) throw error;
var yearObject = {},
monthObject = {},
monthCount = 0;
for (var i = 0; i < result.length; i++) {
var date = new Date(ObjectId(result[i]._id).getTimestamp());
var previousDate = new Date(
ObjectId(result[i == 0 ? 0 : i - 1]._id).getTimestamp()
);
date.getMonth() !== previousDate.getMonth()
? (monthCount = 0)
: monthCount++;
var month = date.getMonth();
monthObject[monthNames[month]] = monthCount;
yearObject[date.getFullYear()] = monthObject;
}
/*result.forEach(function(doc) {
var date = new Date(ObjectId(doc._id).getTimestamp());
var month = date.getMonth() + 1;
monthCount++;
monthObject[month] = monthCount;
yearObject[date.getFullYear()] = monthObject;
});*/
callback(yearObject);
db.close();
});
}
);
};