MongoDB 常用查询语句
MongoDB常用查询语句
·
- 数据库工具:Navicat
- id :1-7 createTime是字符串类型,8-13 createTime是date类型
直接贴代码:
查所有
db.getCollection("memberReadHistory").find()
条件范围查询
db.getCollection('memberReadHistory').find({"memberId":3})
db.getCollection('memberReadHistory').find({"memberId":3,"productId":500})
db.getCollection('memberReadHistory').find({"memberId":{$gt:2}})
db.getCollection('memberReadHistory').find({"memberId":{$gt:2},"productId":500})
使用 $and 多条件查询
db.getCollection('memberReadHistory').find({$and:[{"memberId":3},{"createTime":{$gte:"2022-08-07 14:38:00",$lte:"2022-08-07 14:40:00"}}]})
时间字符串范围查询
db.getCollection('memberReadHistory').find({"memberId":3,"createTime":{$gte:'2022-08-07 14:38:00',$lte:'2022-08-07 14:40:00'}})
//date时间查询,ISODate("2022-08-07T07:22:44.375Z")
db.getCollection('memberReadHistory').find({$and:[{"memberId":3},{"createTime":{$gte:ISODate("2022-08-07T07:22:44.375Z")}}]})
根据条件查重并去重
db.getCollection("XXXXXXXXX").aggregate(
[
{
$match: {
taskId: 2
}
}, {
$group: {
_id: {
zbjId: '$zbjId',
taskId: '$taskId',
createTime: '$createTime'
},
count: {
$sum: 1
},
dups: {
$addToSet: '$_id'
}
}
},
{
$match:{count:{$gt:1}}
}])
.forEach(function(it){
it.dups.shift();
db.getCollection("VipUserScoreRecord").remove({_id:{$in:it.dups}});
});
解释一下:aggregate 是聚合函数,第一个match相当于where,第二个match相当一having,group _id里面是分组条件,forEach函数里面是查重之后的去重操作。
更多推荐
所有评论(0)