Getting Started with MongoDB

Why learn MongoDB?
The traditional SQL way of operating the database is too complicated, there are too many problems to consider, and it is easy to make mistakes
MongoDB's Bson way to operate the database is easy to learn and easy to use
As long as the design is reasonable, the efficiency of MongoDB can far exceed MySql
Completely open source and free
Fully compatible with various programming languages

What is MongoDB?
MongoDB is an open source database system based on distributed file storage.
In the case of high load (more storage space and stronger processing power), adding more nodes (this is called sharding) can guarantee server performance.
MongoDB is a collection-oriented document database, and the basic concepts involved are different from relational databases.
MongoDB
Relational Database

DataBase
DataBase
database
Collection
Table
Database table/collection
Document/BSON Document
Record/Row
Database record row/document
field
Column
data field/domain
Index
Index
index

table joins
Table joins, not supported by MongoDB


MongoDB Documentation
Document is the core concept of MongoDB, which is essentially a JSON-like BSON format data.
BSON is a JSON-like binary format data, which can be understood as adding some new data types on the basis of JSON, including date, int32, int64, etc.
BSON is composed of a group of key-value pairs, which has three characteristics of lightness, traversability and efficiency. Traversability is the main reason MongoDB uses BSON as a data store.
BSON official website address: http://bsonspec.org/
The following issues need to be noted when working with MongoDB documents:
①The atomicity of write operations in MongoDB is limited to the document level, and the saving, modification, and deletion of documents are all atomic operations
②The storage space occupied by a single document cannot exceed 16MB
③ MongoDB will try to keep the order of key-value pairs when documents are inserted (the order of key-value pairs will be changed during update)
④Key-value pairs are ordered
    {"type":1,"status":2}不等于{"status":2,"type":1}
    Document case information is sensitive
    Duplicate keys cannot appear in the document
    The value of a document can be not only a string, but several other types, or even the entire embedded document

The following are several data types commonly used in MongoDB
type of data

String

Integer

Boolean

Double

Min/Max keys
Compare a value to the lowest and highest value of a BSON element
Array
Used to store an array or list or multiple values ​​as a key
Timestamp
timestamp. Record when the document was modified or added
Object ID

Null
for creating empty values
Symbol
符号。该数据类型基本等同于字符串类型,但不同的是,它一般采用特殊符号类型的语言
Date
日期时间。用UNIX时间格式来存储当前日期或时间
Object
用于内嵌文档
Binary Data
二进制数据
Code
代码类型。用于在文档中存储JavaScript代码
Regular expression
正则表达式类型

关于文档键的命名需要注意以下几点:
①_id事故系统保留的关键字,它是默认的主键,该值在集合中必须唯一,且不可更改。
②键名不能包含\0或空字符,这个字符用于表示键的结尾
③不能以$开头
④不能包含.(点号)

MongoDB集合
集合就是一组文档,类似于关系数据库中的表。
集合是无模式的,集合中的文档可以是各式各样的。它们的键值可以不同,值的类型也可以不同,但是它们可以存放在同一个集合中,也就是不同模式的文档都可以放在同一个集合中。
但是如果把各种模式的文档放在同一个集合中,对于开发者来说无疑是灾难的,集合难以管理,对集合的查询、索引等操作效率都不高,在获取到查询结果后还需过滤掉各种不同类型的文档。
所以在实际的使用中,往往将文档分类放在不同的集合中。
这种对文档进行划分来分别存储的模式并不是MongoDB的强制要求,用户可以灵活选择。
可以使用“.”按照命名空间将集合划分为子集合。虽然子集合没有任何特殊的地方,但是使用子集合组织数据结构更清晰。
关于集合的命名需要注意以下几点:
①不能使空字符串
②不能含有\0字符或空字符,这个字符用于表示集合的结尾
③不能以"system."开头,这是为系统集合保留的前缀
④用户创建的集合名字不能含有保留字符。有些驱动程序的确支持在集合名里面包含,这是因为某些系统和生成的集合中包含该字符。除非你要访问这种系统穿件的集合,否则千万不要在名字里出现$

元数据
数据库的信息是存储在集合中的。它们使用了系统的命名空间:
dbname.system.*
在MongoDB数据库中名字空间<dbname>.system.*是包含多种系统信息的特殊集合(Collection)
集合命名空间 描述
dbname.system.namespaces 列出所有名字空间。
dbname.system.indexes 列出所有索引。
dbname.system.profile 包含数据库概要(profile)信息。
dbname.system.users 列出所有可访问数据库的用户。
dbname.local.sources 包含复制对端(slave)的服务器信息和状态。

MongoDB数据库
MongoDB中多个文档组成集合,多个集合组成数据库。
一个MongoDB实例可以承载多个数据库。它们之间可以看做相互独立,每个数据库都有独立的权限控制。在磁盘上,不同的数据库存放在不同的文件中。
MongoDB中存在以下系统数据库:
①Admin:权限数据库,如果创建用户的时候将该用户添加到admin数据库中,那么该用户就自动继承了所有数据库的权限。
②Local:这个数据库永远不会被复制,可以用来存储本地单台服务器的任意集合。
③Config:当MongoDB使用分片模式时,config数据库在内部使用,用于保存分片的信息


MongoDB文档的嵌入和引用
对于关系型数据库,不同类型的信息需存入不同的表中。查询某些关联的信息时,需要检索多个表。
但是对于mongoDB或者其他非关系型数据库来说,可以将关联的信息嵌入在单一的文档中。
每个MongoDB文档都由BSON文档组成,有类似JSON格式一样的数据类型,其中String、Int、Float称为基本类型(或常量),而Hash和Array称之为复合类型。
所谓的嵌套,就是说文档中,利用复合类型,包裹一个多或多个其他类型的值,这些值称之为子文档。
文档嵌套的数量和深度没有限制,但MongoDB目前版本限制一个文档最大为16MB。
采用这种方法维持了数据逻辑上的完整性,可以将一整项数据作为一个整体来操纵。对比在关系型的数据库中,为了设计出符合规范的表,我们常常要将多个数据项才分为几个表,然后通过外键来获取数据。这样做的后果是,当我们只看一个单独的表的数据时,通常只能看到一部分数据,而其他的都是外键id,影响了可读性和逻辑的完整性。
检索时,只需要数据从一个文档加载到内存中,不必加载多个文档,提高了检索效率。

mongodb是介于关系型与非关系型数据库之间的,mongodb的join查询可以通过引用来实现。
mongodb的引用是通过额外的执行一次查询来解决的。
mongodb提供了两种方式来实现:手动引用和使用DBRef标准
①手动引用
手动引用就是在文档中插入要引用文档的id,检索时,通过_id进行一次额外的检索就可以获取所需的信息了
②DBRef
DBRef提供了一个更正式的规范引用文档之间的数据
在DBRef中,数据库引用以标准的JSON/ BSON嵌入对象存储的。
语法: 
{ $ref : <collectionname>, $id : <id value>[, $db : <database name>] }
<collectionname>代表引用的集合的名称。
<id value>所引用的文档的_id值。
$ db是可选的,引用的文档所在的数据库的名称。

参考:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481216&siteId=291194637