限于nodejs的异步特性,在进行异步方式配合回调是非常好的解决方案
node 版本
node -v
v12.10.0
// Documentation: https://mongodb.github.io/node-mongodb-native/3.1/api/
新增:
exports.insertOne = insertOne;
/**
*添加一条记录
*
* @param {*} collection
* @param {*} value
*/
function insertOne(collection, value, next) {
MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("enforce");
dbo.collection(collection).insertOne(value, function(err, res) {
if (err) throw err;
next({ "err": err, "res": res });
});
});
}
删除:
exports.deleteOne = deleteOne;
function deleteOne(collection, where, next) {
MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("enforce");
dbo.collection(collection).deleteOne(where, function(err, obj) {
if (err) throw err;
db.close();
});
});
}
传入对象=并按照id进行更新:
exports.updateOneBy_id = updateOneBy_id;
/**
* 更新某一条记录
* @param {} collection
* @param {*} where
* @param {*} values
*/
function updateOneBy_id(collection, singleResult, next) {
let _id = singleResult._id;
delete singleResult._id;
MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("enforce");
dbo.collection(collection).updateOne({ "_id": _id }, { $set: singleResult }, function(err, res) {
if (err) throw err;
db.close();
next({ "err": err, "res": res });
});
});
}
根据某一字段更新:
exports.updateOne = updateOne;
/**
* 更新某一条记录
* @param {} collection
* @param {*} where
* @param {*} values
*/
function updateOne(collection, where, values, next) {
MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("enforce");
var updateStr = { $set: values };
dbo.collection(collection).updateOne(where, updateStr, function(err, res) {
if (err) throw err;
db.close();
next({ "err": err, "res": res });
});
});
}
简单查询:
exports.selectFirstOne = selectFirstOne;
/**
*查询数据,如果返回为{}则无数据
*
* @param {*} collection
* @param {*} where
*/
function selectFirstOne(collection, where, next) {
MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("enforce");
dbo.collection(collection).find(where).toArray(function(err, result) { // 返回集合中所有数据
if (err) throw err;
db.close();
if (result.length > 0) {
next(result[0]);
} else {
next({});
}
});
});
}
exports.selectWhere = selectWhere;
/**
* 通过条件查询数据
* @param {*} collection 集合
* @param {*} where 查询条件
*/
function selectWhere(collection, where, next) {
MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
var dbo = db.db("enforce");
dbo.collection(collection).find(where).toArray(function(err, result) { // 返回集合中所有数据
if (err) throw err;
db.close();
next(result);
});
});
}
复杂查询的where条件构造:需要注意的是如果想查询子对象,需要先将最深层的子对象的定义放在查询的首位,当然这个有点low,但是一定能达到你想要的父子结构和树结构的查询效果
let where = {};
if (req.query.search) {
var search = req.query.search;
if (search.a) {
where = { "object.ip": { '$regex': search.a, '$options': 'i' } };
}
if (search.b) {
where.b= { '$regex': search.b, '$options': 'i' };
}
} else {
where = {};
}