mongoUtil查询封装与使用:
0.经常使用的网址:
mongoDB: https://docs.mongodb.com/v3.6/reference/operator/aggregation/match/#pipe._S_match
monk: https://automattic.github.io/monk/docs/collection/aggregate.html
菜鸟教程: https://www.runoob.com/mongodb/mongodb-databases-documents-collections.html
1.install monk:
npm intall monk
2.mongoUtils.js封装:
const monk = require('monk');
//monk带密码连接mongo,不加authSource=admin可能会导致连接失败
const db = monk('root:pwd@192.168.31.136:27017/app?authSource=admin');
module.exports = {
save: function (data, collectionName, callback) {
//获得指定的集合
const collection = db.collection(collectionName);
collection.insert(data)
.then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
find: function (where, set, collectionName, callback) {
const collection = db.collection(collectionName);
collection.find(where, set) .then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
findAll: function (collectionName, callback) {
const collection = db.collection(collectionName);
collection.find()
.then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
findOne: function (where, set, collectionName, callback) {
const collection = db.collection(collectionName);
collection.findOne(where, set).then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
del: function (where, collectionName, callback) {
const collection = db.collection(collectionName);
collection.remove(where).then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
count: function (where, collectionName, callback) {
const collection = db.collection(collectionName);
collection.count(where).then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
update: function (where, set, collectionName, callback) {
const collection = db.collection(collectionName);
collection.update(where, set).then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
findOneAndUpdate: function (where, set, collectionName, callback) {
const collection = db.collection(collectionName);
collection.findOneAndUpdate(where, set).then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
aggregate: function (where, collectionName, callback) {
const collection = db.collection(collectionName);
collection.aggregate(where).then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
drop: function (collectionName, callback) {
const collection = db.collection(collectionName);
collection.drop().then((docs) => {
callback(null, docs)
}).catch((err) => {
callback(err, null)
});
},
};
3.test.js测试使用【仅列举一些常用的】:
const mongo = require("../utils/mongoUtils");
3.1【查询全部文档】
mongo.findAll(【collectionName】, (err, data) => {
if (data && data.length > 0) {
data.forEach((v, i) => {
...
});
}
});
3.2【根据条件删除文档】
mongo.del({_id: id}, 【collectionName】, (err, data) => { })
3.3 【drop删除集合的文档,删除集合本身,删除在集合上创建的索引。】
mongo.drop(【collectionName】);
3.4【添加文档】
mongo.save(【jsonObj】,【collectionName】, (err, data) => { })
3.5【根据主键,修改局部内容】
mongo.update({_id: id}, {$set: {"isUse": isUse}}, 【collectionName】, (err, data) => { })
3.6【查询一条数据,也可以使用findOne,此处插寻结果为array需要取array[0]】
mongo.find({_id: id}, null, 【collectionName】, (err, data) => {})
3.7【in】查询多条数据
let array = [];
mongo.find({"_id": {$in: array}}, null, 【collectionName】, (err, data) => {})
3.8【分页,排序,模糊匹配查询,获取指定字段】
//示例格式:等同于以下sql查询: (id = 444 or id like %444%) 此种方式是为了在不知道该数据类型时使用
//【mysql语句】
//select id,org_code,stu_code,pk_price,create_time
//from xxx
//where (id = 444 or id like %444%) and (pk_price=111.01 or pk_price like %111.01%)
// and create_time>="xxx" and create_time<="xxx"
//order by _id desc limit 0,10
//【对应的mongo语句】
wheres = {
"$and": [{
"$or": [{
"id": 444
}, {
"id": 444
}, {
"id": {
"$regex": ".*?444.*"
}
}]
}, {
"$or": [{
"pk_price": 111.01
}, {
"pk_price": 111.01
}, {
"pk_price": {
"$regex": ".*?111.01.*"
}
}]
}, {
"create_time": {
"$gte": "2019-10-31T16:00:00.000Z",
"$lte": "2019-11-05T15:00:00.000Z"
}
}]
} {
"projection": {
"id": 1,
"org_code": 1,
"stu_code": 1,
"pk_price": 1,
"create_time": 1
},
"skip": 0,
"limit": 10,
"sort": {
"_id": -1
}
}
let wheres = {};
wheres.isUse = isUse;
wheres.confName = {$regex: '.*?' + confName + '.*'};
//如果此处需要添加动态属性,则使用wheres[xxx]进行属性填充
let x1 = "动态属性1";
let x2 = "动态属性2";
wheres[x1] = "内容1";
wheres[x2] = "内容2"
mongo.find(wheres, {
//projection是获取指定字段关键字,confId是字段名称,1表示仅查询此字段,结果中会含有_id
projection: {"confId":1},
skip: (pageNum - 1) * pageSize,
limit: pageSize,
sort: {"createDate": -1}
}, 【collectionName】, (err, data) => { })
3.9【查询列和】:
let ss = "111.01";
require1.aggregate( [
{ $match : {$or: [ {"pk_price" : ss},{"pk_price" : +ss},{"pk_price" : {$regex: '.*?' + ss + '.*'}}]}},
// { $match : {"pk_price" : {$regex: '.*?' + ss + '.*'}}},
{ $group : { _id : null, "id" : {$sum : "$id"}, "pk_price" : {$sum : "$pk_price"} }}
],【collectionName】,(err,data) => {
console.error(err);
console.error(data);
});
4.0【文档计数】:
// require1.count({id:"1100000"},【collectionName】,(err,data) => {
// console.error(err);
// console.error(data);
// });