mongodb常用查询

 

版本:

4.2.13

 

1.按照pusher_type分组统计

db.getCollection('tb_test01').aggregate([
{ "$group": {
"_id": "$pusher_type",
"pusher_type": { "$sum": 1 }
}}
])

 

 

2.按照月份分组( create_time为ISODate("2023-08-02T11:23:11Z")的格式)

db.t_push_record.aggregate([
{$group:{
_id:{year:{$year: "$create_time"},month:{$month:"$create_time"}},
count:{$sum:1}
}
}
])

或是采用如下方式

 

db.tb_test01.aggregate(
 [
  {$project:{day:{$substr:['$create_time',0,10]}}},
  {$group:{_id:"$day", count:{"$sum": 1 }}}

]);

 

 

 

 

 

3.查找创建时间最小值:

db.t_push_record.find().sort({create_time: 1}).limit(1);

 

4.获取表上第一条数据(根据字段排序)

db.checkpoint_writes.find({"created_at": { $exists: true, $ne: null }}).sort({ created_at: 1 }).limit(1);

 

5.查找字段不存在的记录

db.checkpoints.find({ created_at: null } ).count();

 

posted @ 2022-06-30 11:11  slnngk  阅读(169)  评论(0)    收藏  举报