MongoDB学习笔记

MongoDB学习笔记

MongoDB 概念解析

SQL术语/概念 MongoDB术语/概念 解释/说明

database database 数据库

table collection 数据库表/集合

row document 数据记录行/文档

column field 数据字段/域

index index 索引

table joins 表连接,MongoDB不支持

primary key primary key 主键,MongoDB自动将_id字段设置为主键

show dbs

local 0.078GB

test 0.078GB
db

test
use local

switch to db local
db.collection.findOne()
db.collection.find()
db.collection.find(query, type)
MongoDB默认将时区设置为格林威治标准时间(GMT),而中国所在的时区是东八区(UTC+8),所以将时间+8小时

var startTime = new Date('2023-10-10 07:30:00')
var endTime = new Date('2023-10-10 19:30:00')

startTime.setHours(startTime.getHours() + 8)
endTime.setHours(endTime.getHours() + 8)

db.collection.find(
    {
        createTime: {
            $gte: startTime,
            $lte: endTime
        }
    }
).sort({
    createTime: -1
})

聚合管道Aggregation.newAggregation操作记录

1、聚合简介

MongoDB中聚合通常用来处理数据,如分组求和、求平均值和排序等,对实现数据复杂操作较为方便,简单来说:聚合就是通过对集合中的数据进行运算,转换为自己需要的形式。

与上篇文章使用MongoTemplate操作数据相比较,Aggregation聚合操作显得更加有优势和便捷,代码清晰简洁,优化查询语句。

2、聚合管道简介

简明:在Linux中,管道一般是将当前命令的执行结果作为下个命令执行的参数。

MongoDB聚合管道:将MongoDB文档在一个管道处理完毕后,将结果传递给下一个管道处理。简单来说:管道就是聚合的整个运算过程。

3、聚合管道常用的表达式(常用)

表达式	    功能	                                等价SQL
$match	    过滤数据,输出符合条件文档	            where
$project    修改输入文档结构(筛选展示文档的键)    个人理解(select)
$limit	    限制计算文档结果返回数	                limit
$sort       文档排序                                order by
$group      文档分组                                group by
$skip       跳过指定数量的文档	                    skip
$unwind     展开数组(数组内容拆分显示)	        无

聚合与sql的对应区别

原生			Java				    SQL                     example

db.collection   Entity.class            from                    db.collection

$match			Aggregation.match		where                   db.collection.aggregate([{ $match: { checkId: { $lt: 10 }}}, { $limit: 10 }])

$project        Aggregation.project     select                  db.collection.aggregate([{ $project: { _id: 1, checkId: 1, teamName: 1 }}, { $limit:100 }])

$group          Aggregation.group       group by                db.collection.aggregate([{ $group : { _id : "$teamName"}}])

$group          Aggregation.group       group by                db.collection.aggregate([{ $group : { _id : "$teamName", mycount : { $sum : 1 }}}])

$group          Aggregation.group       group by 多列分组       db.collection.aggregate([{ $group : { _id : { "teamName": "$teamName","taskName": "$taskName" }, mycount : { $sum : 1 }}}])

$sort           Aggregation.sort        order by                db.collection.aggregate([{ $group : { _id : "$teamName", chmycountckId : { $sum : 1 }}}, { $sort: { chmycountckId: 1 } }]) - 升序

$skip           Aggregation.skip        limit n, m              db.collection.aggregate([{ $group : { _id : "$teamName", mycount : { $sum : 1 }}}, { $sort: { checkId: 1 } }, { $skip: 1 }])

$limit          Aggregation.limit       limit n, m              db.collection.aggregate([{ $group : { _id : "$teamName", mycount : { $sum : 1 }}}, { $sort: { checkId: 1 } }, { $skip: 1 }, { $limit: 10 }])

$count          Aggregation.count       count(*)                直接嵌入到查询的返回{ "$count": "count" }

mongodb使用了JavaScript Shell,加减乘除还有其他啥的查询一些动态值可以通过js的方式进行一些计算,如

var num = Math.random() * 10

db.collection.aggregate([{ $match: { checkId: { $gt: num }}}, { $limit: 10 }])

相关推荐
Moonnnn.16 分钟前
51单片机学习——动态数码管显示
笔记·嵌入式硬件·学习·51单片机
南宫生1 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
技术小齐1 小时前
网络运维学习笔记 016网工初级(HCIA-Datacom与CCNA-EI)PPP点对点协议和PPPoE以太网上的点对点协议(此处只讲华为)
运维·网络·学习
竹言笙熙1 小时前
代码审计初探
学习·web安全
日记成书1 小时前
物联网智能项目
物联网·学习
虾球xz2 小时前
游戏引擎学习第118天
学习·游戏引擎
gz927cool2 小时前
大模型做导师之开源项目学习(lightRAG)
学习·开源·mfc
电棍2333 小时前
verilog笔记
笔记·fpga开发
让我安静会3 小时前
Obsidian·Copilot 插件配置(让AI根据Obsidian笔记内容进行对话)
人工智能·笔记·copilot
世事如云有卷舒4 小时前
FreeRTOS学习笔记
笔记·学习