MongoDB常用命令记录
- 查询重复记录
db.study.aggregate([
{$group:
{_id : {patientId: '$patientId', orgCode: '$orgCode'},
count: { $sum : 1 } }
},
{$match:
{ "_id.orgCode": "123" ,
count: { $gt : 1}}
}])
- 查找orgCode字段为空:
db.study.find({orgCode:{$in:["",null]}});
- 修改:
db.study.update(
{patientId:"CT",studyUid:"1"},
{$set:{orgCode:"123"}}
);
- 同步study表的orgCode到series
db.series.find({orgCode:{$in:["",null]}}).forEach(function(x){
var studyData = db.study.findOne({patientId:x.patientId,studyUid:x.studyUid});
if (studyData!=null){
db.series.update(
{_id:x._id},
{$set :{orgCode:studyData.orgCode}}
);
}
})
- 统计内嵌文档的数组长度
db.series.aggregate(
{$match: {seriesUid:"1.2.392.200036.9116.2.6.1.37.2417506717.1543793972.511807"}},
{$unwind: "$fileList"},
{$project: {count:{$add:1}}},
{$group: {_id: null, number: {$sum: "$count" }}}
)
- MongoDB 导出远程数据库到本地:
mongoexport --host 172.16.40.174 --port 27017 --collection table --db ydy_db --out C:\Users\99324\Desktop\table .json
- MongoDB导入到本地:
mongoimport --host 127.0.0.1 --port 27017 --db ydy_db --collection table --file C:\Users\99324\Desktop\table .json
- 更新内嵌文档:
var seriesList = db.series.find({partStatus:0,taskType:"XrayDetect"});
seriesList.forEach(function(series){
var fileList = series.fileList;
if (fileList.length>1){
var num = 1;
fileList.forEach(function(file){
file.fileAiStatus=NumberInt(0);
file.filePartStatus=NumberInt(0);
file.fileTaskType="UNKNOWN";
file.instanceNo=String(num);
num++;
})
db.series.update({"_id":series._id},{$set:{"fileList":fileList}});
}
})
注意:更新如果字段类型是数字,像file.instanceNo字段,需要使用NumberInt。
如果字段类型是字符串类型的数字,像