From 062d90056364507ba2fd3d8633a8270cbff433e4 Mon Sep 17 00:00:00 2001 From: Qionglu735 Date: Mon, 7 Aug 2023 08:18:47 +0000 Subject: [PATCH] MongoDB 5.7.0 --- lib/document_stores/mongo.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/document_stores/mongo.js b/lib/document_stores/mongo.js index 502a508..f48c916 100644 --- a/lib/document_stores/mongo.js +++ b/lib/document_stores/mongo.js @@ -12,29 +12,31 @@ MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { var now = Math.floor(new Date().getTime() / 1000), that = this; - this.safeConnect(function (err, db) { + this.safeConnect(function (err, db, close) { if (err) return callback(false); - db.collection('entries').update({ + db.collection('entries').updateOne({ 'entry_id': key, $or: [ { expiration: -1 }, { expiration: { $gt: now } } ] - }, { + }, {$set :{ 'entry_id': key, 'value': data, 'expiration': that.expire && !skipExpire ? that.expire + now : -1 - }, { + }}, { upsert: true - }, function (err, existing) { + }).then(function (existing, err) { if (err) { winston.error('error persisting value to mongodb', { error: err }); + close(); return callback(false); } callback(true); + close(); }); }); }; @@ -43,7 +45,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { var now = Math.floor(new Date().getTime() / 1000), that = this; - this.safeConnect(function (err, db) { + this.safeConnect(function (err, db, close) { if (err) return callback(false); @@ -53,7 +55,7 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { { expiration: -1 }, { expiration: { $gt: now } } ] - }, function (err, entry) { + }).then(function (entry, err) { if (err) { winston.error('error persisting value to mongodb', { error: err }); return callback(false); @@ -70,18 +72,18 @@ MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { } }, function (err, result) { }); } + close(); }); }); }; MongoDocumentStore.prototype.safeConnect = function (callback) { - MongoClient.connect(this.connectionUrl, function (err, db) { - if (err) { - winston.error('error connecting to mongodb', { error: err }); - callback(err); - } else { - callback(undefined, db); - } + const client = new MongoClient(this.connectionUrl); + let db_name = this.connectionUrl.split("/"); + db_name = db_name[db_name.length - 1]; + const db = client.db(db_name); + callback(undefined, db, function() { + client.close(); }); };