blog-server/bin/mongodb.js
2019-11-13 20:17:51 +08:00

166 lines
5.5 KiB
JavaScript

var MongoClient = require('mongodb').MongoClient;
var dburl;
let add = (set, data, url = dburl) => {
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
try {
dbo.collection(set).insertOne(data, function (err, res) {
if (err) throw err;
db.close();
resolve({ code: 0 });
});
} catch (error) {
reject({ code: 1, error })
}
});
})
}
let adds = (set, data, url = dburl) => {
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
try {
dbo.collection(set).insertMany(data, function (err, res) {
if (err) throw err;
db.close();
resolve({ code: 0, num: res.insertedCount });
});
} catch (error) {
reject({ code: 1, error })
}
});
})
}
let remove = (set, where, url = dburl) => {
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
try {
dbo.collection(set).deleteOne(where, function (err, obj) {
if (err) throw err;
db.close();
resolve({ code: 0 });
});
} catch (error) {
reject({ code: 1, error })
}
});
})
}
let removes = (set, where, url = dburl) => {
return new Promise((resolve, reject) => {
if (where == "" || Object.keys(where).length == 0) {
reject({ code: 1 })
}
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
try {
dbo.collection(set).deleteMany(where, function (err, obj) {
if (err) throw err;
db.close();
resolve({ code: 0, num: obj.result.n });
});
} catch (error) {
reject({ code: 1, error })
}
});
})
}
let find = (set, where = {}, page = 0, num = 0, url = dburl) => {
return new Promise((resolve, reject) => {
if (num == 0) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
try {
dbo.collection(set).find(where).toArray(function (err, result) {
if (err) throw err;
resolve({ code: 0, data: result });
db.close();
});
} catch (error) {
reject({ code: 1, error })
}
});
} else {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
try {
dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
if (err) throw err;
resolve({ code: 0, data: result });
db.close();
});
} catch (error) {
reject({ code: 1, error })
}
});
}
})
}
let update = (set, where = {}, data, defin = true, url = dburl) => {
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
if (defin) {
data = { $set: data }
}
try {
dbo.collection(set).updateOne(where, data, function (err, res) {
if (err) throw err;
resolve({ code: 0 });
db.close();
});
} catch (error) {
reject({ code: 1, error })
}
});
})
}
let updates = (set, where = {}, data, url = dburl) => {
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
data = { $set: data }
try {
dbo.collection(set).updateMany(where, data, function (err, res) {
if (err) throw err;
db.close();
resolve({ code: 0, num: res.result.nModified });
});
} catch (error) {
reject({ code: 1, error })
}
});
})
}
module.exports = function (url, dbname) {
dburl = { url, dbname };
return {
add,
adds,
remove,
find,
update,
updates,
removes
}
}