MongoDB笔记(七) —— 聚合

语法:

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
> db.demo.find()
{ "_id" : ObjectId("5e79a57ba3edd4aa4ff0c2c3"), "name" : "hisName", "age" : 20, "hobby" : [ "drawing", "run" ] }
{ "_id" : ObjectId("5e79a878a3edd4aa4ff0c2c4"), "name" : "yourName", "age" : "30", "hobby" : [ "drawing", "run" ] }
> 
> db.demo.aggregate([{$group:{_id:"$name",num_tutorial:{$sum:1}}}])
{ "_id" : "yourName", "num_tutorial" : 1 }
{ "_id" : "hisName", "num_tutorial" : 1 }
> 

类似sql语句:

select name as _id, count(*) as num_tutorial from demo group by name
表达式 描述 实例
$sum 计算总和。 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", num_tutorial : { s u m : " sum : " likes"}}}])
$avg 计算平均值 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", num_tutorial : { a v g : " avg : " likes"}}}])
$min 获取集合中所有文档对应值得最小值。 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", num_tutorial : { m i n : " min : " likes"}}}])
$max 获取集合中所有文档对应值得最大值。 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", num_tutorial : { m a x : " max : " likes"}}}])
$push 在结果文档中插入值到一个数组中。 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", url : { p u s h : " push: " url"}}}])
$addToSet 在结果文档中插入值到一个数组中,但不创建副本。 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", url : { a d d T o S e t : " addToSet : " url"}}}])
$first 根据资源文档的排序获取第一个文档数据。 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", first_url : { f i r s t : " first : " url"}}}])
$last 根据资源文档的排序获取最后一个文档数据 db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "name", last_url : { l a s t : " last : " url"}}}])
  • 聚合框架中常用的几个操作:

    • $project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
    • ** m a t c h match**:用于过滤数据,只输出符合条件的文档。 match使用MongoDB的标准查询操作。
    • $limit**:用来限制MongoDB聚合管道返回的文档数。
    • $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
    • $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
    • $group:将集合中的文档分组,可用于统计结果。
    • $sort:将输入文档排序后输出。
    • $geoNear:输出接近某一地理位置的有序文档。
  • 时间关键字如下:

    • $dayOfYear: 返回该日期是这一年的第几天(全年 366 天)。
    • $dayOfMonth: 返回该日期是这一个月的第几天(1到31)。
    • $dayOfWeek: 返回的是这个周的星期几(1:星期日,7:星期六)。
    • $year: 返回该日期的年份部分。
    • $month: 返回该日期的月份部分( 1 到 12)。
    • $week: 返回该日期是所在年的第几个星期( 0 到 53)。
    • $hour: 返回该日期的小时部分。
    • $minute: 返回该日期的分钟部分。
    • $second: 返回该日期的秒部分(以0到59之间的数字形式返回日期的第二部分,但可以是60来计算闰秒)。
    • $millisecond:返回该日期的毫秒部分( 0 到 999)。
    • $dateToString: { $dateToString: { format: , date: } }。

管道操作符实例

1、$project实例

db.article.aggregate(
    { $project : {
        title : 1 ,
        author : 1 ,
    }}
 );

这样的话结果中就只还有_id,tilte和author三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:

db.article.aggregate(
    { $project : {
        _id : 0 ,
        title : 1 ,
        author : 1
    }});

2.$match实例

db.articles.aggregate( [
                        { $match : { score : { $gt : 70, $lte : 90 } } },
                        { $group: { _id: null, count: { $sum: 1 } } }
                       ] );

m a t c h 70 90 match用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段 group管道操作符进行处理。

3.$skip实例

db.article.aggregate(
    { $skip : 5 });

经过$skip管道操作符处理后,前五个文档被"过滤"掉。

发布了90 篇原创文章 · 获赞 12 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/u012382791/article/details/105425575