var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = 'mongodb://localhost' // connect url

function mongodbUpdate (dbname, collection, findStr, updateStr) {
return new Promise((resolve, reject) => {
var findStr1 = findStr;
var updateStr1 = updateStr;
MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
}, function (err, db) {
if (err) throw err;
var dbo = db.db(dbname);
dbo.collection(collection).updateMany(findStr1, updateStr1, function (err, res) {
if (err) throw err;
console.log(res.result.nModified + ' updated');
resolve(res.result);
db.close();
})
})
})
};

function mongodbDelete (dbname, collection, object_id) {
return new Promise((resolve, reject) => {
console.log(collection);
MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
}, function (err, db) {
if (err) throw err;
var dbo = db.db(dbname);
var queryStr = { object_id: new ObjectID(object_id) }
dbo.collection(collection).deleteMany(queryStr, function (err, res) {
if (err) throw err;
console.log(res.result.n + ' removed');
resolve(res.result);
db.close();
})
})
});
};

module.exports = { mongodbUpdate, mongodbDelete };