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();
業務需求變更永無休止,技術前進就永無止境!

浙公網安備 33010602011771號