浏览器标题切换
浏览器标题切换end

MongoDB - 聚合(aggregate)操作

前置知识

  • 聚合英文:aggregate

  • 聚合的方法:aggregate()

  • 聚合作用:求给定数据的总和、平均值等。

  • 聚合语法:db.集合名.aggregate(聚合操作)

具体例子

  1. 往集合里面插入多个文档:
db.Article.insertMany([
{
	book_name: 'MongoDB',
	description: 'MongoDB is a NoSQL database.',
	author: 'Alice',
	evaluation: 100,
	tags: ['Database', 'NoSQL']
},{
	book_name: 'MySQL',
	description: 'MySQL is a relational database.',
	author: 'Alice',
	evaluation: 90,
	size:{ height:10, weight:15}
},{
	book_name: 'PostgreSQL',
	description: 'pqsql balabalalalalala.',
	author: 'John',
	evaluation: 80
}])
undefined
  1. 执行 db.Article.aggregate([{$group : {_id: "$author", article_number: {$sum: 1}}}]) ,统计出每个人写的文章数

    undefined

  2. 给Alice多增加一篇文章 把mysql的作者mike->Alice:db.Article.update({book_name: 'MySQL'},{$set:{author: 'Alice'}})

    undefined

  3. 再次执行 db.Article.aggregate([{$group : {_id : "$author", article_number : {$sum : 1}}}]) 命令,观察变化。

    undefined

参考

https://www.runoob.com/mongodb/mongodb-aggregate.html2.

posted @   抓水母的派大星  阅读(265)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2020-11-12 CodeForces681C - Heap Operations - 优先队列+模拟
点击右上角即可分享
微信分享提示