数据库
首页 > 数据库> > MongoDb查询

MongoDb查询

作者:互联网

//数据转换类型mongodb过滤数据并聚合
db.IotMeterGasUsedRecord_2020.aggregate([{
   // 获取字段 
  $project: {
        _id: 0,
     // 类型转换
        RecordUsedValue: {
            $toDouble: {
                $substrBytes: ["$RecordUsedValue", 0, 4]
            }
        },
        RecordUserId: 1
    }
}, {
    $group: {
        _id: "$RecordUserId",
        RecordUsedValue: {
            $sum: ("$RecordUsedValue")
        }
    }
}]);
//过滤重复数据
db.IotMeterUnReportRecord_2021.aggregate([
    {
        $group:{_id:{RecordUserId:'$RecordUserId',RecordYear:'$RecordYear',RecordMonth:'$RecordMonth',RecordDay:'$RecordDay'},count:{$sum:1},dups:{$addToSet:'$_id'}}
    },
    {
        $match:{count:{$gt:1}}
    }

    ])    
    .forEach(function(it){
      // 所动首条
         it.dups.shift();
            db.IotMeterUnReportRecord_2021.remove({_id: {$in: it.dups}});

    });
db.IotMeterUnReportRecord_2020.aggregate([
    {
        $group:{_id:{RecordUserId:'$RecordUserId',RecordYear:'$RecordYear',RecordMonth:'$RecordMonth',RecordDay:'$RecordDay'},count:{$sum:1},dups:{$addToSet:'$_id'}}
    },
    {
        $match:{count:{$gt:1}}
    }

    ]).forEach(function(it){

         it.dups.shift();
            db.IotMeterUnReportRecord_2020.remove({_id: {$in: it.dups}});

    });

 

 

 

标签:RecordUsedValue,dups,MongoDb,db,查询,RecordDay,RecordUserId,id
来源: https://www.cnblogs.com/happen-/p/14582082.html