MongoDB简单查询语句<平时使用语录,持续更新>

MongoDB查询语句


--查询近三个月的客户使用量  aggregate:使用聚合  match:过滤  group分组   
-- mysql中
select org_code as 近三个月使用商户 from pomelo_backend_production.landi_configurations
where created_at between '2018-03-22 00:00:00' and '2018-06-22 00:00:00'
GROUP BY org_code;

--mongodb中  db.表名.aggregate([])
db.tuan_activities.aggregate([
{$match : { created_at : { $gt : ISODate("2018-03-22T00:00:00.000Z"), $lte : ISODate("2018-06-31T00:00:00.000Z") } } },
{$group : {_id : "$org_code", num_tutorial : {$sum : 1}}}
])




-- 查询近三个月所有数据
-- mysql中
select count(*) as 近三个月数据 from pomelo_backend_production.landi_configurations
where created_at between '2018-03-22 00:00:00' and '2018-06-22 00:00:00';

--mongodb中
db.getCollection('tuan_activities').find({ "created_at" : { "$gte" :ISODate("2018-03-22T00:00:00.000Z"),
    "$lt" : ISODate("2018-06-31T00:00:00.000Z")}}).sort({created_at:-1})

-- 分组
db.getCollection('tuan_activities').aggregate([
{
    $group:{
            _id: '$org_code',
             num_tutorial : {$sum : 1}
       }
}
]
)

原文地址:https://www.cnblogs.com/lwh-note/p/9224226.html