-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectionDriverGaia.js
488 lines (454 loc) · 21.1 KB
/
collectionDriverGaia.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
var ObjectID = require('mongodb').ObjectID;
// convert any incoming strings to ObjectIDs if they’re to be used when comparing against an “_id” field.
/********************************************
Entries should look like this:
{ _id: ,
time_created: ,
Coordinates: [longitude (floar), latitude(float)] ,
Title: string,
Category: [String] ,
Rank: ,
Media: [ Source: facebook
Text:
Tags:
Image_url:
Link:
Rating: },
{ Source: instagram
Text:
Tags:
Image_url:
Link:
Rating: }]
}
********************************************/
// self - current context
CollectionDriver = function(db) {
this.db = db;
};
CollectionDriver.prototype.removeCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else {
the_collection.remove({}, function(error, results) {
if (error) callback(error);
else callback(null, results);
});
}
});
}
CollectionDriver.prototype.addLocation2dsphereIndex = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else {
the_collection.ensureIndex( { 'loc.coordinates': "2dsphere", 'rank': -1 }, {bits: 32}, function(error, results) { // this returns a write concern obj
if (error) callback(error);
else callback(null, results);
});
}
});
}
CollectionDriver.prototype.getCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, the_collection) {
if( error ) callback(error);
else callback(null, the_collection);
});
};
CollectionDriver.prototype.findAll = function(collectionName, callback) {
this.getCollection(collectionName, function(error, the_collection) { //A
if( error ) callback(error);
else {
the_collection.find().toArray(function(error, results) { //B
if( error ) callback(error);
else callback(null, results);
});
}
});
};
CollectionDriver.prototype.findInBoxGivenCategory = function(collectionName, minlon, maxlon, minlat, maxlat, category, callback) {
console.log("params: " + minlon + " " + minlat + " " + maxlon + " " + maxlat);
this.getCollection(collectionName, function(error, the_collection) {
if (error) {
callback(error);
} else {
the_collection.find({'loc.coordinates' :
{$geoWithin :
{$geometry :
{type : "Polygon" ,
coordinates : [[[minlon, minlat] ,
[minlon, maxlat],
[maxlon, maxlat],
[maxlon, minlat],
[minlon, minlat]]]
}
}
}
, 'category' : category}).toArray( function(error, results) { // this should return only category has an element matching...
if (error)
callback(error);
else
callback(null, results); });
}
});
}
CollectionDriver.prototype.findInBox = function(collectionName, minlon, maxlon, minlat, maxlat, callback){
console.log("params: " + minlon + " " + minlat + " " + maxlon + " " + maxlat);
this.getCollection(collectionName, function(error, the_collection) {
if (error) {
callback(error);
} else {
the_collection.find({'loc.coordinates' :
{$geoWithin :
{$geometry :
{type : "Polygon" ,
coordinates : [[[minlon, minlat] ,
[minlon, maxlat],
[maxlon, maxlat],
[maxlon, minlat],
[minlon, minlat]]]
}
}
}
}).toArray( function(error, results) {
if (error)
callback(error);
else
callback(null, results); });
}
});
}
CollectionDriver.prototype.findInCircle = function(collectionName, center_lon, center_lat, min_dist, max_dist, callback) {
console.log("collectionName: " + collectionName);
console.log("Params: " + center_lon + ", " + center_lat + " with distance between meters " + min_dist + " , " + max_dist);
this.getCollection(collectionName, function(error, the_collection) {
if (error) {
callback(error);
} else {
console.log("try to find in this collection: " + collectionName);
the_collection.find({
geoNear: "loc.coordinates",
near: { type: "Point", coordinates: [ center_lon, center_lat] },
spherical: true,
query: { }, // ex) query: { category: "public" },
minDistance: min_dist, // in meters
maxDistance: max_dist
}).toArray(function(error, results) {
if (error)
callback(error);
else
callback(null, results); });
}
});
}
CollectionDriver.prototype.get = function(collectionName, id, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
if (!checkForHexRegExp.test(id)) callback({error: "invalid id"});
else the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) {
if (error) callback(error);
else callback(null, doc);
});
}
});
};
CollectionDriver.prototype.getTop = function(collectionName, minlon, maxlon, minlat, maxlat, category, num, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
console.log("ready to getTop");
// db.things.find().sort({someField:1}).limit(10)
//.sort({'rank': -1 }).limit(10)
the_collection.find({'loc.coordinates' :
{$geoWithin :
{$geometry :
{type : "Polygon" ,
coordinates : [[[minlon, minlat] ,
[minlon, maxlat],
[maxlon, maxlat],
[maxlon, minlat],
[minlon, minlat]]]
}
}
}
, 'category' : category}).sort({'rank': -1 }).limit(num).toArray( function(error, results) { // this should return only category has an element matching...
if (error)
callback(error);
else
callback(null, results); });
}
});
}
/**************** INSERT/SAVE ****************/
//insert/save one item
CollectionDriver.prototype.save = function(collectionName, obj, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if( error ) callback(error)
else {
// obj = filter_item(obj);
obj.time_created = new Date();
the_collection.insert(obj, function() {
callback(null, obj);
});
}
});
};
//save an array of objects
CollectionDriver.prototype.saveBulk = function(collectionName, obj, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if( error ) callback(error)
else {
var date = new Date();
for (var i = 0; i < obj.length; i++) {
// obj[i] = filter_item(obj[i]);
obj[i].time_created = date;
}
the_collection.insert(obj, function() {
callback(null, obj);
});
}
});
};
/**************** UPDATE ****************/
//update a specific object
// covers over the existing one?
CollectionDriver.prototype.update = function(collectionName, obj, entityId, callback) {
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
obj._id = ObjectID(entityId);
obj.updated_at = new Date();
the_collection.save(obj, function(error,doc) {
if (error) callback(error);
else callback(null, obj);
});
}
});
};
// ADD this media item info to the media array
// essentially each post is an item
//
CollectionDriver.prototype.addCategoryBulk = function(collectionName, category_array, id, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) {
if (error) callback(error);
else {
var array = doc.category;
var new_array = [];
for (var i = 0; i < category_array.length; i++) {
if ( array.indexOf(category_array[i]) == -1 ) { // does not exist in the array
new_array.push(category_array[i]);
}
}
// { $push: { scores: { $each: [ 90, 92, 85 ] } } }
var update = { $push : {} };
update.$push['category'] = { $each: new_array};
the_collection.update( {'_id':ObjectID(id)}, update,
function(error,doc) {
if (error) callback(error);
else callback(null, doc);
});
}
});
}
}
});
};
// ADD this media item info to the media array
// essentially each post is an item
CollectionDriver.prototype.addMedia = function(collectionName, media_item, source, id, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
// update( {'_id' : id, { $push : {'media.' + source : media_item } } })
var update = { $push : {} };
update.$push['media.' + source] = media_item;
the_collection.update( {'_id':ObjectID(id)}, update,
function(error,doc) {
if (error) callback(error);
else callback(null, doc);
});
}
}
});
};
// ADD this media item info to the media array
// essentially each post is an item
CollectionDriver.prototype.addMediaBulk = function(collectionName, media_item, source, id, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
console.log("tryyyyying");
console.log('media.' + source);
console.log("id: " + id);
// { $push: { scores: { $each: [ 90, 92, 85 ] } } }
// update( {'_id' : id, { $push : {'media.' + source : media_item } } })
// .find( { $where: "this.name.length > 1" } );
the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) {
if (error) callback(error);
else {
console.log("find one done");
var obj = doc;
if (obj.media[source]) { // if does exist...
console.log("Media " + source + " exists. Do nothing. ");
// the_collection.update( {'_id':ObjectID(id)},
// {$set : { 'media.' + source : media_item} } );
callback(null, doc);
} else {
console.log("Media " + source + " does not exists. Simply add.");
var update = { $push : {} };
update.$push['media.' + source] = { $each: media_item};
the_collection.update( {'_id':ObjectID(id)}, update, function(error,doc) {
if (error) callback(error);
else callback(null, doc);
});
}
}
});
}
}
});
};
// update this given media item
// Should it over cover all the media item (now) OR should it be adding to the media fields individually
CollectionDriver.prototype.updateMedia = function(collectionName, media_item, id, entityId, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
the_collection.update({"media.source" : media_item.source}, {$set : {"media.$" : media_item}});
/*
// an alternative: find and then modify
the_collection.findOne({'_id':ObjectID(id)}, function(error,doc) {
if (error) {
callback(error);
} else {
// push a media
var media_array = doc.media;
for (var i = 0; i < media_array.length; i++) {
if (media_array[i].source == media_item.source) {
var key = "media." + i;
the_collection.update( {'_id':ObjectID(id)},
{$set: { key: media_item} );
}
}
}
// no such entry in the media. does this works?
CollectionDriver.prototype.addMedia = function(collectionName, media_item, id, entityId, callback);
});
*/
}
}
});
};
// update rank to increase or decrease by the given inc_value
CollectionDriver.prototype.incrementRank = function(collectionName, inc_value, id, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
// find which index on the array has the same source
id = "550a78cf8029f27c169d9b32";
console.log("id: " + id);
console.log("incvalue: " + inc_value);
the_collection.update( {'_id':ObjectID(id)},
{ $inc: { 'rank': inc_value } }, function(error,doc) {
if (error) callback(error);
else {
console.log(doc);
callback(null, doc);
}});
}
}
});
};
// update the essential fields of the item in case of emergency
// field can be 'title', 'category', 'coordinates'
CollectionDriver.prototype.updateRank = function(collectionName, field_value, id, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
// find which index on the array has the same source
the_collection.update( {'_id':ObjectID(id)},
{$set : { 'rank' : field_value} }, function(error,doc) {
if (error) callback(error);
else {
console.log(doc);
callback(null, doc);
}
});
}
}
});
};
// update the essential fields of the item in case of emergency
// field can be 'title', 'category', 'coordinates'
CollectionDriver.prototype.updateField = function(collectionName, field, field_value, id, callback) {
var obj = null;
this.getCollection(collectionName, function(error, the_collection) {
if (error) callback(error);
else {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); //B
if (!checkForHexRegExp.test(id)) {
callback({error: "invalid id"});
} else {
console.log("aaaa");
// find which index on the array has the same source
the_collection.update( {'_id':ObjectID(id)},
{$set : { field : field_value} }, function(error,doc) { //B
if (error) callback(error);
else {
console.log("booom");
console.log(doc);
callback(null, doc);
}
});
}
}
});
};
/**************** DELETE ****************/
//delete a specific object
CollectionDriver.prototype.delete = function(collectionName, entityId, callback) {
this.getCollection(collectionName, function(error, the_collection) { //A
if (error) callback(error);
else {
the_collection.remove({'_id':ObjectID(entityId)}, function(error,doc) { //B
if (error) callback(error);
else callback(null, doc);
});
}
});
};
exports.CollectionDriver = CollectionDriver;