MongoDB小技巧总结

1 mongodb索引操作

2 mongodb内嵌查询

嵌套对象查询

{"report_content.measurement_judgment.enabled":true}

  

3 mongodb更新操作

parame1:查询条件

param2:局部更新字段

param3:false代表不存在时是否要新增记录

param4:是否批量更新,为false时只更新检索到的第一条数据

db.collection.update({"field":"value"},{$set:{'field':'value'}},false,true);

4 mongdb更新操作:将B字段赋值给A字段

db.eval(function() { 
    db.collection.find().forEach(function(e) {
        e.phyHealthReuslt = e.phyModelReuslt;
        e.phyHealthPoint = e.phyModelPoint;
        db.collection.save(e);
    });
});

 

5 修改字段的数据类型

NumberLong类型转换为string类型  

db.sleepReportV2.find({"reportId":{$type:18}}).forEach(
    function(x){
        x.reportId =  x.reportId + "";
        // 这行代码不行,将Long字段207转换成字符串NunberLong(207)
        //x.reportId = String(x.reportId);
        db.sleepReportV2.save(x)
    });

  

db.sleepReportV2.find({"reportId":{$type:18}}).count();

  

业务需求变更永无休止,技术前进就永无止境!
原文地址:https://www.cnblogs.com/yucongblog/p/15682277.html