TOP:老版本MONGDB
db.version(); 2.6.11
#分组
db.getCollection('InvestorAuth').group(
{
keyf: function(doc){
return {'uid':doc.uid.toString() };
},
cond:{ $and:[ {"status" : "PASS"} ,{ "createdAt": {$gt: ISODate("2018-12-31T16:00:00.000Z")} } ]},
reduce: function ( curr, result ) {
result.count++;
if(result.auditAt<curr.auditAt) {
result.auditAt = new Date(curr.auditAt)
result.investorRoleEnum = curr.investorRoleEnum
result.position = curr.position
result.orgName = curr.orgName
}
var date = new Date(result.auditAt);
var dateKey = '' + date.getFullYear()+'-'+ (date.getMonth()+1) +'-'+ date.getDate()+' '
+(date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
+(date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
+(date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
result.dateKey=dateKey
},
initial: { count:0 ,'auditAt':0 }
}
)
#全量
var arr=[];
db.getCollection('InvestorAuth').find({"createdAt": {$gt: ISODate("2020-07-01T16:00:00.000Z"),$lt: ISODate("2020-09-01T16:00:00.000Z")}},{_id:0,uid:1,realName:1,createdAt:1}).sort({createdAt:-1})
.forEach(function(result){
var date1 = new Date(result.createdAt);
var dateKey1 = '' + date1.getFullYear()+'-'+ (date1.getMonth()+1) +'-'+ date1.getDate()+' '
+(date1.getHours() < 10 ? '0' + date1.getHours() : date1.getHours()) + ':'
+(date1.getMinutes() < 10 ? '0' + date1.getMinutes() : date1.getMinutes()) + ':'
+(date1.getSeconds() < 10 ? '0' + date1.getSeconds() : date1.getSeconds());
result.createdAt=dateKey1
arr.push(result)
})
print(arr)
#分组按时间处理
db.InvestorAuth.group(
{
keyf: function(doc){
var date = new Date(doc.createdAt);
var dateKey = '' + date.getFullYear()+ '-' + (date.getMonth()+1) + '-' + date.getDate();
return {'day':dateKey};
},
cond:{ $and:[{"authTypeEnum" : 'FIRST_AUTH_NO_RELATED'},{"status" : "PASS"},
{
"send_time": { $gte:ISODate("2018-05-01T16:00:00.000Z"),$lt:ISODate("2021-03-17T16:00:00.000Z") }
} ]},
reduce: function ( curr, result ) {
result.count++;
},
initial: { count:0}
}
)
# mongdb 多条件分组
db.InvestorAuth.group(
{
keyf: function(doc){
var date = new Date(doc.auditAt);
var dateKey = '' + date.getFullYear()+ '-' + (date.getMonth()+1) + '-' + date.getDate();
status = doc.status
auditor = doc.auditor
return {'day':dateKey,'auditor': auditor, 'status' : status};
},
cond:{ $and:[{"status" : {$in:["PASS","REJECT"]}},{"auditAt" : {"$ne":null, $exists:true}}, //sendcloud 邮件统计,只统计发送成功的
{
"auditAt": { $gte:ISODate("2021-04-25T16:00:00.000Z"),$lt:ISODate("2021-05-25T16:00:00.000Z") }
} ]},
reduce: function ( curr, result ) {
result.count++;
},
initial: { count:0}
}
)
item.privateAttrs[index].seq = new NumberInt(item.privateAttrs[index].seq);
#便于转换EXCEL查询
var arr=[];
db.getCollection('ActivityV2').find({},{_id:0,sessions:0}).sort({createdAt:-1})
.forEach(function(result){
arr.push(result)
})
print(arr)
#时间查询
var arr=[];
db.getCollection('MobilCompanyBpUpdateBean').find({createAt:{'$gt':ISODate("2021-08-12T16:00:00.000Z")},createAt:{'$lt':ISODate("2021-08-13T16:00:00.000Z")},auditStatus:'PENDING'},{_id:1,uid:1,ccid:1}).sort({_id:-1}).limit(300).forEach(function(result){
arr.push(result)
})
print(arr)
更新语句:
单条更新
db.MobilCompanyHeaderUpdateBean.update(
{createAt:{'$gt':ISODate("2021-08-12T16:00:00.000Z"),'$lt':ISODate("2021-08-13T16:00:00.000Z")},auditStatus:'PENDING'},
{$set:{'auditStatus':'PASS'}
)
多条更新
db.MobilCompanyHeaderUpdateBean.update(
{createAt:{'$lt':ISODate("2021-02-13T16:00:00.000Z")},auditStatus:'PENDING'},
{$set:{'auditStatus':'REJECT'}},{multi:true})
查询是否存在
db.getCollection('company_create_record').find({"base.industry": {"$in": [null], "$exists": true}}).sort({_id:-1})
mongdb 索引:
创建索引:
db.getCollection('InvestorAuth').ensureIndex({"uid":NumberInt(1)})
db.getCollection('InvestorAuth').createIndex({ "authTypeEnum" : NumberInt(1)}, { "name" : "authTypeEnum" })
删除索引:
db.getCollection('InvestorAuth').dropIndex("uid_1")
查看索引:
db.getCollection('InvestorAuth').getIndexes()