-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoDB.js
124 lines (100 loc) · 3.21 KB
/
mongoDB.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
/**
* Module Dependencies
* */
var mongodb = require('mongodb');
/**
* MongoDB conf
* */
var HOST = "107.170.232.222";
var PORT = 27017;
var DB = "lovelace";
/**
* Insert operation
*
* @data Array of the objects to be inserted on mongoDB
* */
var insert = function(idCar, data, callback){
updateCollectionProperties(idCar, data, function(err, db, client, collection){
if(err){
console.log("Error updating the collection properties of: " + collection, err);
}
client.collection(idCar).insert(data, {}, function(err, records){
db.close();
callback(err, records);
});
});
};
/**
* Updates the collection properties of the car: new_collection.properties.idCar
* */
var updateCollectionProperties = function(collection, last_trip, callback){
delete last_trip.isochrone;
var server = new mongodb.Server(HOST, PORT);
var db = new mongodb.Db(DB, server);
db.open(function (err, client) {
if (err){
callback(err, db, client, null);
}
var idCar = collection;
client.collection(idCar).findOne({ collectionProperties: true }, function(err, propertiesDocument){
if(err){
console.log("Error getting the properties object.", err);
}
var collectionProperties = propertiesDocument;
if(!propertiesDocument){
collectionProperties = {
collectionProperties: true,
distance: 0,
total_time: 0,
stop_time: 0
};
}
collectionProperties.distance += last_trip.properties.distance;
collectionProperties.total_time += last_trip.properties.total_time;
collectionProperties.stop_time += last_trip.properties.stop_time;
client.collection(idCar).findOneAndReplace({ collectionProperties: true }, collectionProperties, {upsert: true}, function(err, result){
callback(err,db,client,result);
});
});
});
};
var getCollections = function(callback){
var server = new mongodb.Server(HOST, PORT);
var db = new mongodb.Db(DB, server);
db.open(function (err, client) {
if (err){
console.log("Error ", err);
}
db.listCollections().toArray(function(err, collectionsNames){
db.close();
if(err){
console.log("Error getting the collection names", err);
}else{
callback(collectionsNames);
}
});
});
};
var getCollectionData = function(filter, collection, callback){
var server = new mongodb.Server(HOST, PORT);
var db = new mongodb.Db(DB, server);
db.open(function (err, client) {
if (err){
console.log("Error ", err);
}
client.collection(collection).find(filter).toArray(function(err, result){
db.close();
callback(err,result);
});
});
};
/**
*
* DAO
*
* */
module.exports = {
insert: insert,
getCollections: getCollections,
getCollectionData: getCollectionData
};