-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
170 lines (146 loc) · 4.52 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var Mongo = function(url)
{
var MongoClient = require('mongodb').MongoClient;
var http = require("http");
this.url = url;
this.CreateDb = function()
{
return new Promise(function(res, rej)
{
MongoClient.connect(url, function(err, db)
{
if (err) throw err;
console.log("Database created!");
});
})
}
// this.CreateCollection() = function(CollectionName)
// {
// return new Promise(res,err =>
// {
// MongoClient.connect(this.url, function(err, db)
// {
// db.createCollection(CollectionName, function(err, res)
// {
// if (err) throw err;
// console.log("Collection created!");
// });
// })
// })
// }
this.Insert = function(collectionName, dataObject)
{
return new Promise(function(res, rej)
{
MongoClient.connect(url, function(err, db)
{
if (err) throw err;
db.collection(collectionName).insertOne(dataObject, function(err, res)
{
if (err) reject(err);
console.log("1 document inserted");
});
})
})
}
this.ReturnAll = function(collectionName)
{
return new Promise(function(res, rej)
{
MongoClient.connect(this.url, function(err, db)
{
if (err) reject(err);
db.collection(collectionName).findOne({}, function(err, result)
{
if (err) reject(err);
resolve(result);
});
})
})
}
this.Query = function(collectionName, query)
{
return new Promise(function(res, rej)
{
MongoClient.connect(this.url, function(err, db)
{
if (err) reject(err);
db.collection("customers").find(query).toArray(function(err, result)
{
if (err) reject(err);
resolve(result);
});
})
})
}
this.Delete = function(collectionName, query)
{
return new Promise(function(res, rej)
{
MongoClient.connect(this.url, function(err, db)
{
if (err) reject(err);
db.collection(collectionName).deleteOne(query, function(err, obj)
{
if (err) reject(err);
console.log("1 document deleted");
});
});
})
}
this.DropCollection = function(collection, name)
{
return new Promise(function(res, rej)
{
MongoClient.connect(this.url, function(err, db)
{
if (err) reject(err);
db.collection(collection).drop(function(err, delOK)
{
if (err) reject(err);
if (delOK) console.log("Collection deleted");
});
});
})
}
this.Update = function(collection, query, newValues)
{
return new Promise(res,rej)
{
MongoClient.connect(this.url, function(err, db)
{
if (err) reject(err);
db.collection(collection).updateOne(query, newvalues, function(err, res)
{
if (err) reject(err);
console.log("1 document updated");
});
});
}
}
//Join needs some work, very confusing for me
// this.Join = function(collection, fromObject, toObject)
// {
// return new Promise(res, rej)
// {
// MongoClient.connect(this.url, function(err, db)
// {
// if (err) reject(err);
// db.collection('orders').aggregate([
// { $lookup:
// {
// from: 'products',
// localField: 'product_id',
// foreignField: 'id',
// as: 'orderdetails'
// }
// }
// ], function(err, res) {
// if (err) throw err;
// resolve(res);
// });
// });
// }
// }
}
module.exports = Mongo;