当前位置:网站首页>《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。
边栏推荐
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- 数字滚动带动画
- Getting started with typescript
- MySQL
- 《重构:改善既有代码的设计》读书笔记(上)
- Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
- Quanzhi A33 uses mainline u-boot
- Microservice technology - distributed global ID in high concurrency
- Markdown基础语法
- C文件输入操作
猜你喜欢

Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
Bubble sort array

Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config

What is 9D movie like? (+ common sense of dimension space)

Talk about the design of red envelope activities in e-commerce system

Processing strategy of message queue message loss and repeated message sending

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

《重构:改善既有代码的设计》读书笔记(上)

High frequency interview questions

In pytorch function__ call__ And forward functions
随机推荐
Microservice technology - distributed global ID in high concurrency
452-strcpy、strcat、strcmp、strstr、strchr的实现
Yunna | why use the fixed asset management system and how to enable it
虚拟机初始化脚本, 虚拟机相互免秘钥
搭建哨兵模式reids、redis从节点脱离哨兵集群
数字滚动带动画
C文件输入操作
Markdown basic grammar
2022.7.1-----leetcode. two hundred and forty-one
《架构整洁之道》读书笔记(下)
End to end object detection with transformers (Detr) paper reading and understanding
PHP asymmetric encryption method private key and public key encryption and decryption method
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
AcWing 1134. 最短路计数 题解(最短路)
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
AcWing 343. 排序 题解(floyd性质实现传递闭包)
Emmet基础语法
Data dimensionality reduction factor analysis
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5