MongoDB常用命令记录

MongoDB常用命令记录

  1. 查询重复记录
db.study.aggregate([
{$group:
	{_id : {patientId: '$patientId', orgCode: '$orgCode'},
	count: { $sum : 1 } }
},
{$match: 
	{ "_id.orgCode": "123" ,
	count: { $gt : 1}}
}])
  1. 查找orgCode字段为空:
db.study.find({orgCode:{$in:["",null]}});
  1. 修改:
db.study.update(
{patientId:"CT",studyUid:"1"},
{$set:{orgCode:"123"}}
);
  1. 同步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}}
    		);
    	}
})
  1. 统计内嵌文档的数组长度
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" }}}
)
  1. MongoDB 导出远程数据库到本地:
mongoexport --host 172.16.40.174 --port 27017 --collection table --db ydy_db --out C:\Users\99324\Desktop\table .json
  1. MongoDB导入到本地:
mongoimport --host 127.0.0.1 --port 27017 --db ydy_db --collection table --file C:\Users\99324\Desktop\table .json
  1. 更新内嵌文档:
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。
如果字段类型是字符串类型的数字,像
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值