mongdb函数 查询 分组 处理时间 索引

这篇博客展示了MongoDB的各种数据操作,包括分组查询、全量数据获取、按时间处理的数据分组以及多条件分组。此外,还涉及了数据更新语句和查询存在性的方法。同时,提到了MongoDB的索引创建、删除和查看,强调了索引在提升查询性能中的作用。
摘要由CSDN通过智能技术生成

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值