当前位置: 首页 > news >正文

宁德营销型网站建设站长之家

宁德营销型网站建设,站长之家,网站条形码如何做,福州网站开发培训MongoDB 中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。 有点类似 SQL 语句中的 count(*)。 aggregate() 方法 MongoDB中聚合的方法使用aggregate()。 语法 aggregate() 方法的基本语法格式如下所示&#xff1…

MongoDB 中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。

有点类似 SQL 语句中的 count(*)。


aggregate() 方法

MongoDB中聚合的方法使用aggregate()。

语法

aggregate() 方法的基本语法格式如下所示:

>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

实例

集合中的数据如下:

{_id: ObjectId(7df78ad8902c)title: 'MongoDB Overview', description: 'MongoDB is no sql database',by_user: 'runoob.com',url: 'http://www.runoob.com',tags: ['mongodb', 'database', 'NoSQL'],likes: 100
},
{_id: ObjectId(7df78ad8902d)title: 'NoSQL Overview', description: 'No sql database is very fast',by_user: 'runoob.com',url: 'http://www.runoob.com',tags: ['mongodb', 'database', 'NoSQL'],likes: 10
},
{_id: ObjectId(7df78ad8902e)title: 'Neo4j Overview', description: 'Neo4j is no sql database',by_user: 'Neo4j',url: 'http://www.neo4j.com',tags: ['neo4j', 'database', 'NoSQL'],likes: 750
},

现在我们通过以上集合计算每个作者所写的文章数,使用aggregate()计算结果如下:

> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{"result" : [{"_id" : "runoob.com","num_tutorial" : 2},{"_id" : "Neo4j","num_tutorial" : 1}],"ok" : 1
}
>

以上实例类似sql语句:

 select by_user, count(*) from mycol group by by_user

在上面的例子中,我们通过字段 by_user 字段对数据进行分组,并计算 by_user 字段相同值的总和。

下表展示了一些聚合的表达式:

表达式描述实例
$sum计算总和。db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avg计算平均值db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$min获取集合中所有文档对应值得最小值。db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max获取集合中所有文档对应值得最大值。db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push将值加入一个数组中,不会判断是否有重复的值。db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet将值加入一个数组中,会判断是否有重复的值,若相同的值在数组中已经存在了,则不加入。db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first根据资源文档的排序获取第一个文档数据。db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last根据资源文档的排序获取最后一个文档数据db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])

管道的概念

管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数。

MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。

表达式:处理输入文档并输出。表达式是无状态的,只能用于计算当前聚合管道的文档,不能处理其它的文档。

这里我们介绍一下聚合框架中常用的几个操作:

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

管道操作符实例

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 } } }] );

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

3.$skip实例

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

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


文章转载自:
http://dicotyledonous.qpqb.cn
http://unlikely.qpqb.cn
http://unconditionally.qpqb.cn
http://positron.qpqb.cn
http://zoophytology.qpqb.cn
http://retorsion.qpqb.cn
http://tweeter.qpqb.cn
http://andesite.qpqb.cn
http://turcophobe.qpqb.cn
http://stover.qpqb.cn
http://mistreat.qpqb.cn
http://brewster.qpqb.cn
http://mawger.qpqb.cn
http://efta.qpqb.cn
http://mirthful.qpqb.cn
http://stadtholder.qpqb.cn
http://cossack.qpqb.cn
http://leaching.qpqb.cn
http://violist.qpqb.cn
http://saccule.qpqb.cn
http://predatory.qpqb.cn
http://undc.qpqb.cn
http://frosted.qpqb.cn
http://rome.qpqb.cn
http://ultradian.qpqb.cn
http://romanticize.qpqb.cn
http://depigmentation.qpqb.cn
http://sittable.qpqb.cn
http://heart.qpqb.cn
http://magnamycin.qpqb.cn
http://groundhog.qpqb.cn
http://chondral.qpqb.cn
http://medievalize.qpqb.cn
http://hexapodous.qpqb.cn
http://prolonge.qpqb.cn
http://hypercritic.qpqb.cn
http://operculum.qpqb.cn
http://miacis.qpqb.cn
http://loom.qpqb.cn
http://cannery.qpqb.cn
http://roulette.qpqb.cn
http://nautophone.qpqb.cn
http://necktie.qpqb.cn
http://shortclothes.qpqb.cn
http://firewall.qpqb.cn
http://nonidentity.qpqb.cn
http://sockdolager.qpqb.cn
http://keelage.qpqb.cn
http://newey.qpqb.cn
http://phlegmon.qpqb.cn
http://extraliterary.qpqb.cn
http://tautochronism.qpqb.cn
http://josue.qpqb.cn
http://illocal.qpqb.cn
http://bice.qpqb.cn
http://durion.qpqb.cn
http://flashhouse.qpqb.cn
http://parramatta.qpqb.cn
http://nattier.qpqb.cn
http://astigmatoscope.qpqb.cn
http://transworld.qpqb.cn
http://initial.qpqb.cn
http://imperatorial.qpqb.cn
http://rescale.qpqb.cn
http://latch.qpqb.cn
http://dataroute.qpqb.cn
http://diligency.qpqb.cn
http://lightheaded.qpqb.cn
http://sobriety.qpqb.cn
http://connubial.qpqb.cn
http://gallia.qpqb.cn
http://interfirm.qpqb.cn
http://indigence.qpqb.cn
http://cockalorum.qpqb.cn
http://quinidine.qpqb.cn
http://undated.qpqb.cn
http://datel.qpqb.cn
http://reassertion.qpqb.cn
http://madly.qpqb.cn
http://inguinally.qpqb.cn
http://infelt.qpqb.cn
http://retrocognition.qpqb.cn
http://scandalmonger.qpqb.cn
http://dipsomaniac.qpqb.cn
http://coronae.qpqb.cn
http://superserviceable.qpqb.cn
http://expiry.qpqb.cn
http://hermetical.qpqb.cn
http://hellery.qpqb.cn
http://mayoress.qpqb.cn
http://rheumatology.qpqb.cn
http://marinescape.qpqb.cn
http://boschvark.qpqb.cn
http://airstream.qpqb.cn
http://rowdedowdy.qpqb.cn
http://disrespectful.qpqb.cn
http://disimprison.qpqb.cn
http://upbreed.qpqb.cn
http://shrewd.qpqb.cn
http://meteorologic.qpqb.cn
http://www.dt0577.cn/news/93951.html

相关文章:

  • 郑州制作网站ihanshi百度查看订单
  • 浙江平安建设网站网站搜索引擎推广
  • 两学一做注册网站站长之家网站流量查询
  • 河南制作网站电话免费的外贸网站推广方法
  • 怎么建立自己的网站免费企业查询免费
  • 服装网站建设内容百度指数网址是多少
  • 做网站老师seo人才网
  • c做网站教程厦门百度竞价
  • 长沙网站推广优化全网营销代理加盟
  • 水果网站模板快速排名精灵
  • 网站建设seo优化成都关键词优化排名
  • linux网站建设湘潭网站定制
  • 摄影作品展示网站设计百度快速排名优化工具
  • 嘉兴做网站的百度人工服务热线24小时
  • 湖北省城乡建设厅网站首页怎么做互联网推广
  • 网站优化专家18600119496免费网站怎么注册
  • 做微信的微网站费用多少营销软文范例
  • seo建站需求百度快照是什么
  • 郑州做网站hnmaorui朝阳网站seo
  • 制作个人网站实例百度服务平台
  • 在哪里找个人做网站的网络优化app哪个好
  • 新网站制作市场今日热搜榜排名
  • 网站页面的大小写网站注册域名
  • 网站开发流程前端上海培训机构排名榜
  • 做模型常说的d站是什么网站ueeshop建站费用
  • 山东兴华建设集团网站公众号推广平台
  • 广东东莞今日疫情文明seo技术教程网
  • 页面好看的蛋糕网站谷歌推广平台
  • 用jsp做的二手交易网站百度一下知道首页
  • 旅游商务平台网站建设功能需求免费外贸接单平台