/
mongo.js
74 lines (65 loc) · 2.56 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
const rlequire = require("rlequire");
const slug = rlequire("dendro", "src/utils/slugifier.js");
const isNull = rlequire("dendro", "src/utils/null.js").isNull;
const MongoClient = require("mongodb").MongoClient;
const Logger = rlequire("dendro", "src/utils/logger.js").Logger;
function DendroMongoClient (mongoDBHost, mongoDbPort, mongoDbCollectionName, mongoDbUsername, mongoDbPassword)
{
let self = this;
self.host = mongoDBHost;
self.port = mongoDbPort;
self.collectionName = mongoDbCollectionName;
self.username = mongoDbUsername;
self.password = mongoDbPassword;
}
DendroMongoClient.prototype.connect = function (callback)
{
const self = this;
let url;
if (self.username && self.password && self.username !== "" && self.password !== "" && self.username !== "")
{
url = "mongodb://" + self.username + ":" + self.password + "@" + self.host + ":" + self.port + "/" + self.collectionName;
}
else
{
url = "mongodb://" + self.host + ":" + self.port + "/" + self.collectionName;
}
Logger.log("debug", "Connecting to MongoDB using connection string: " + url);
MongoClient.connect(url, function (err, db)
{
if (!err)
{
return callback(null, db);
}
const msg = "Error connecting to MongoDB " + JSON.stringify(db, null, 4);
return callback(true, msg);
});
};
DendroMongoClient.prototype.findFileByFilenameOrderedByDate = function (db, fileUri, callback)
{
const collection = db.collection("fs.files");
collection.find({filename: fileUri}).sort({uploadDate: -1}).toArray(function (err, files)
{
if (!err)
{
return callback(null, files);
}
const msg = "Error finding document with uri: " + fileUri + " in Mongo. error: " + JSON.stringify(err);
return callback(true, msg);
});
};
DendroMongoClient.prototype.getNonAvatarNorThumbnailFiles = function (db, callback)
{
const collection = db.collection("fs.files");
// db.getCollection('fs.files').find( { $and: [{ "metadata.thumbnail": { $ne: true }} , {"metadata.avatar": { $ne: true }}]})
collection.find({ $and: [{ "metadata.thumbnail": { $ne: true }}, {"metadata.avatar": { $ne: true }}]}).sort({uploadDate: -1}).toArray(function (err, files)
{
if (isNull(err))
{
return callback(null, files);
}
const msg = "Error when looking for non Avatar nor thumbnail files in Mongo. error: " + JSON.stringify(err);
return callback(true, msg);
});
};
module.exports.DendroMongoClient = DendroMongoClient;