184 lines
6.5 KiB
JavaScript
184 lines
6.5 KiB
JavaScript
var MongoClient = require('mongodb').MongoClient;
|
|
var dburl;
|
|
let add = (set,data,url = dburl) =>{
|
|
// console.log(url)
|
|
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
try {
|
|
dbo.collection(set).insertOne(data, function(err, res) {
|
|
if (err) throw err;
|
|
// console.log("文档插入成功");
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
try {
|
|
dbo.collection(set).insertMany(data, function(err, res) {
|
|
if (err) throw err;
|
|
// console.log("文档插入成功");
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
try {
|
|
dbo.collection(set).deleteOne(where, function(err, obj) {
|
|
if (err) throw err;
|
|
// console.log("文档插入成功");
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
try {
|
|
dbo.collection(set).deleteMany(where, function(err, obj) {
|
|
if (err) throw err;
|
|
// console.log("文档插入成功");
|
|
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) =>{
|
|
// console.log(url)
|
|
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
try {
|
|
dbo.collection(set). find(where).toArray(function(err, result) { // 返回集合中所有数据
|
|
if (err) throw err;
|
|
// console.log(result);
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
try {
|
|
dbo.collection(set). find(where).skip(num * page).limit(num).toArray(function(err, result) { // 返回集合中所有数据
|
|
if (err) throw err;
|
|
// console.log(result);
|
|
resolve({code:0,data:result});
|
|
db.close();
|
|
});
|
|
} catch (error) {
|
|
reject({code:1,error})
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
})
|
|
}
|
|
let update = (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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
data = {$set:data}
|
|
try {
|
|
dbo.collection(set).updateOne(where, data, function(err, res) {
|
|
if (err) throw err;
|
|
// console.log(result);
|
|
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);
|
|
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
|
|
data = {$set:data}
|
|
try {
|
|
dbo.collection(set).updateMany(where, data, function(err, res) {
|
|
if (err) throw err;
|
|
// console.log("文档插入成功");
|
|
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
|
|
}
|
|
} |