# 批量修改字段值
db.getCollection('collectiona').find().forEach( function(item){ db.getCollection('collectiona').update({"_id":item._id},{$set:{"outTradeNo1": 'wzf'+item.title}}) } )
db.task_record.update({},{$set:{"downloadAudioFile":true}}, false, true);
# 批量修改字段:从另外一张表查询对应字段进行更新
var cursor = db.getCollection("qualityInspectionInfo").find({"duration":null}); while (cursor.hasNext()){ r = cursor.next(); var resourceInfo = db.getCollection("resourceInfo").find({"taskId":r['taskId']})[0]; db.getCollection("qualityInspectionInfo").update({"taskId":r['taskId']},{$set:{"duration":resourceInfo['attr']['duration']}},false,false); }
# 查找某字段的范围在和不在
# 查找特定字段的取值等于哪些值"$in" ,不等于则用"$nin" db.getCollection('resourceInfo').find({"attr.channel":"test100","attr.seqNo":{"$in":['000','11133','qqqq']}});
# 查询某个字段不存在
db.getCollection('test_log_chatflow').find({"result":{"$exists":false}})
# 查询某个字段存在
db.getCollection('test_log_chatflow').find({"result":{"$exists":true}})
# 根据时间范围查询
db.getCollection('M1SAudioUploadedInfo').find({"seqNo":/^test0915a116.*/,"cloudUploadStatus":{"$in":[0,1,2]},"audioPath":/.mnt.card.test0915a116.*/, "createTime" : {"$gte":ISODate("2020-09-18T14:00:00.233+08:00"),"$lt":ISODate("2020-09-18T14:30:00.233+08:00")}}).count()
# 去重:某个字段查询结果去重后统计
//distinct 去重返回一个数组,length 获取长度。 //distinct 第一个参数是去重字段,第二个参数是筛选条件。 db.getCollection('resourceInfo').distinct("attr.serviceId",{"status":"complete", "attr.voiceTime":{"$gte":ISODate("2021-04-30T23:59:59.000+08:00")}}).length
# 查询某一个字段的最大值
db.resourceInfo.aggregate([{"$group":{_id: null,max_value:{"$max":"$createTime"}}}])
# 分组求平均值,最大值,最小值
db.taskTakeTime.aggregate([ {"$group":{"_id":"$periodName","sum":{"$sum":"$takeSeconds"},"count":{"$sum":1},"avg":{"$avg":"$takeSeconds"}}}] ) db.taskTakeTime.aggregate([ {"$group":{"_id":"$periodName","max":{"$max":"$takeSeconds"},"min":{"$min":"$takeSeconds"}}}] )
# 聚合统计求和
#无查询条件求字段和 db.resourceInfo.aggregate( [ { $group: { _id: null,total: { $sum: "$attr.duration" } } } ] );
#根据查询条件统计
db.test_log_chatflow.aggregate([
{"$match":{"call":true,"result":{"$ne":"hello"}}},
{"$group":{"_id":"$sid","count":{"$sum":1}}}]
)
# 根据查询条件按时间分组统计
db.resourceInfo.aggregate([
{"$match":{"statisticsStatus":"1"}},
{"$group":{"_id":"$attr.voiceTime","count":{"$sum":1}}}
] )
db.test_log_chatflow.aggregate([
{"$match":{"call":true,"result":{"$ne":"hello"}}},
{$group : { _id : '$sid', count : { $sum : 1 }}},
{$match : { count : { $gte : 2 }}}])
# 跟进查询条件求字段和和数据量 db.resourceInfo.aggregate( [ { $match : { "attr.callType":2,"statisticsStatus" : "1","info.voiceTime" : {"$gte":ISODate("2020-12-26T00:00:00.2000+08:00"), "$lt":ISODate("2020-12-27T00:00:00.2000+08:00")} }}, { $group: { _id: null,count: { $sum: 1 },totalTime: { $sum: "$text.totalTime" },validTime: { $sum: "$text.validTime" }}} ] );
db.test_log_chatflow.aggregate([
{ "$project": {
"difference": {
"$divide": [
{ "$subtract": ["$updateAt", "$createAt"] },
1
]
}
}},
{ "$group": {
"_id": "$sid",
"totalDifference": { "$sum": "$difference" } //"$sum" "$avg" "$max" "$min"
}},
{ "$match": { "totalDifference": { "$gte": 0 }}}
])
# 参考文档 https://www.cnblogs.com/zhangzxl/p/4079641.html # 按iso的日期分组统计,加8小时 db.resourceInfo.aggregate([ {"$match":{"statisticsStatus":"2","groupId":1434}}, {"$group": {"_id": {"createDate": { "year": {"$year": {$add:["$createTime",28800000]}}, "month": {"$month": {$add:["$createTime",28800000]}}, "day": {"$dayOfMonth":{$add:["$createTime",28800000]}}}}, "count": { "$sum": 1}}}, {"$sort":{"_id":1}} ] )
# 索引
#删除索引
db.task_record.dropIndex({"createTime": -1}); #创建索引
db.task_record.ensureIndex({"createTime": -1, "voiceTime": -1, "duration": -1}); db.task_record.ensureIndex({"filename": -1});
# 备份数据库表:导出表的json文件
#进入bin目录 cd /data/mongodb/bin # ./mongoexport -h 10.40.7.xx:27017 -d test_jxdx120 -c rule -o ./rule.json
#导入json文件
--导入json文件 mongoimport --db ${db_name} --collection ${collection_name} --file contacts.json ./mongoimport --db test_jxdx120 --collection rule --file /data/mongodb/bin/jsondata/rule.json
参考文档
常用:https://www.cnblogs.com/liu--huan/p/10721266.html
时间差:https://www.it1352.com/1638348.html