当前位置:网站首页>《MongoDB入门教程》第03篇 MongoDB基本概念
《MongoDB入门教程》第03篇 MongoDB基本概念
2022-07-02 18:37:00 【不剪发的Tony老师】
本文将会介绍 MongoDB 中的一些基本概念,例如文档、集合、数据库以及命名空间等。
数据格式
在 MongoDB 中,数据使用 JSON 或者 BSON 格式进行处理和存储。
JSON
JSON 全称为 JavaScript Object Notation,是一种轻量级的数据交换格式。JSON 语法基于 JavaScript ECMA-262 3rd edition 的一个子集。
一个 JSON 文档就是一个由字段和值组成的集合,遵循特定的格式。例如:
{
"first_name": "John",
"last_name": "Doe",
"age": 22,
"skills": ["Programming","Databases", "API"]
}
BSON
BSON 代表 Binary JSON,它是一种类 JSON 文档的二进制序列化。BSON 更注重存储和处理的效率。
文档
MongoDB 以 BSON 文档的形式存储数据记录,简称为文档(Document)。
文档是一个由字段-值组成的集合,结构如下:
{
field_name1: value1,
field_name2: value2,
field_name3: value3,
...
}
以上语法中,字段名是字符串,字段值可以是数字、字符串、对象、数组等。例如:
{
_id: ObjectId("5f339953491024badf1138ec"),
title: "SQL编程思想",
isbn: "9787121421402",
publisher: "电子工业出版社",
published_date: new Date('2021-10-01'),
author: {
first_name: "旭阳", last_name: "董"}
}
该文档包含以下字段-值:
- _id 是一个 ObjectId;
- title 是一个字符串;
- isbn 是一个字符串;
- publisher 是一个字符串;
- published_date 是一个 Date 类型数据;
- author 是一个嵌入式文档,包含了两个字段: first_name 和 last_name。
如果你了解关系型数据,可以看出文档就像是表中的一行记录,但是它比数据行更具有表达性。
文档中的字段名需要遵循以下规则:
- MongoDB 保留字段 _id 用于唯一标识一个文档;
- 字段名不能包含 null 字符;
- 最外层的字段名不能进以 $ 符号开始。
集合
MongoDB 使用集合(Colletion)存储文档。一个集合就是一组文档。
MongoDB 中的集合类似于关系型数据库中的表。
MongoDB | RDBMS |
---|---|
文档 | 行 |
集合 | 表 |
表拥有固定的模式(字段定义),但是集合的模式是动态的。动态模式意味着集合中的多个文档结构可能完全不同。例如,以下文档可以存储在同一个集合中:
{
title: "SQL编程思想",
published_date: new Date('2021-10-01')
}
{
title: "MongoDB从入门到商业实战",
published_date: new Date('2019-09-01'),
isbn": "9787121372247"
}
第2个文档比第1个文档多个一个字段。理论上来说,每个文档都可以有不同的字段。
一个集合拥有一个名字,例如 books。集合名词不能包含以下内容:
- $ 字符;
- null(\0)字符;
- 空字符串;
- 以 system 开头,因为 system* 是 MongoDB 内部集合的保留名。
数据库
MongoDB 集合存储在数据库中。 单个 MongoDB 实例可以包含多个数据库(Database)。
数据库可以通过名字进行引用,例如 bookdb。数据库名不能为以下内容:
- 一个空字符串(“”);
- 包含以下字符:/、\、.、“、*、<、>、:、|、?、$、空格或者 \0(空字符);
- 长度超过 64 字节。
MongoDB 还包含一些保留的数据库名称,例如 admin、local 以及 config,我们不能使用这些名称创建新的数据库。
命名空间
命名空间(Namespace)由数据库名加上其中的集合名组成。命名空间可以用于完整引用一个集合。
例如,对于数据库 bookdb 中的集合 books,命名空间为 bookdb.books。
边栏推荐
- Mobile robot path planning: artificial potential field method [easy to understand]
- mysql备份后缀是什么_mysql备份还原
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 数据降维——因子分析
- Markdown基础语法
- Thread application instance
- Binary operation
- Data dimensionality reduction factor analysis
- Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
- AcWing 1135. 新年好 题解(最短路+搜索)
猜你喜欢
搭建主从模式集群redis
Juypter notebook modify the default open folder and default browser
PyTorch函数中的__call__和forward函数
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
安装单机redis详细教程
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
End to end object detection with transformers (Detr) paper reading and understanding
随机推荐
移动机器人路径规划:人工势场法[通俗易懂]
Which video recording software is better for the computer
metric_logger小解
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
Talk about the design of red envelope activities in e-commerce system
搭建主从模式集群redis
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
PHP asymmetric encryption method private key and public key encryption and decryption method
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
以太网PHY层芯片LAN8720A简介
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
SIFT特征点提取「建议收藏」
metric_ Logger urination
NPOI导出Excel2007
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
《代碼整潔之道》讀書筆記
IEDA refactor的用法
Thread application instance
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失