This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdatabaseOperations.js
More file actions
58 lines (55 loc) · 2.03 KB
/
Copy pathdatabaseOperations.js
File metadata and controls
58 lines (55 loc) · 2.03 KB
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
var MongoClient = require("mongodb").MongoClient;
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('connectionData.json', 'utf8'));
var connectionString = obj.connectionString;
var databaseName = obj.databaseName;
var collectionName = obj.collectionName;
module.exports = {
queryCount: function (callback, errorCallback) {
console.log(`Querying container:\n${collectionName}`);
MongoClient.connect(connectionString, { useNewUrlParser: true }, function (err, client) {
if (err != null) {
errorCallback(err);
return;
}
console.log("Connected correctly to server");
var db = client.db(databaseName);
// Get the documents collection
const collection = db.collection(collectionName);
// Find some documents
collection.count(function (err, count) {
if (err != null) {
errorCallback(err)
}
console.log(`Found ${count} records`);
callback(count);
client.close();
});
});
},
addRecord: function (pageName, callback, errorCallback) {
var milliseconds = (new Date).getTime().toString();
var itemBody = {
"id": milliseconds,
"page": pageName
};
MongoClient.connect(connectionString, { useNewUrlParser: true }, function (err, client) {
if (err != null) {
errorCallback(err);
return;
}
console.log("Connected correctly to server");
var db = client.db(databaseName);
// Get the documents collection
const collection = db.collection(collectionName);
// Insert some documents
collection.insertMany([itemBody], function (err, result) {
if (err != null) {
errorCallback(err)
}
callback();
client.close();
});
});
}
}