• 数据库工具: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函数里面是查重之后的去重操作。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐