A comprehensive summary of the necessary knowledge points of MongoDB

This blog will systematically summarize the necessary knowledge points of MongoDB. After reading this blog, you will be able to: understand MongoDB's business scenarios, characteristics and architecture, data types, etc. Be able to install and start MongoDB under Windows and Linux, install and use the graphical management interface Compass, master the basic common commands of MongoDB to realize CRUD of data, master the index type, index management, and execution plan of MongoDB. (Reminder: The length of this article is long, I suggest you read and learn slowly after collecting it. It is not easy to sort out the code words, please pay attention, like and comment, thank you everyone!)

1. MongoDB related concepts

1. Business application scenarios

We are already very familiar with traditional relational databases, such as MySQL, so when do we need to use MongoDB? The traditional relational database is powerless in the face of the "three high" requirements of data operation and the website requirements of Web2.0.

Three high demands:
(1) The demand for high concurrent read and write of the database.
(2) The need for efficient storage and access to massive data.
(3) The need for high scalability and high availability of the database.

And MongoDB can cope with the "three high" requirements.

The specific application scenarios of MongoDB are as follows:
(1) Social scenarios , using MongoDB to store user information and Moments information published by users, and realize functions such as nearby people and places through geographic location indexing.
(2) The game scene uses MongoDB to store game user information, and the user's equipment and points are directly stored in the form of embedded documents, which is convenient for query, efficient storage and access.
(3) In the logistics scenario , MongoDB is used to store order information, and the order status will be continuously updated during the delivery process, stored in the form of an embedded array in MongoDB, and all changes in the order can be read out in one query.
(4) In the Internet of Things scenario , MongoDB is used to store all connected smart device information, as well as log information reported by the device, and perform multi-dimensional analysis on these information.
(5) Live video , using MongoDB to store user information, like interactive information, etc.
insert image description here

In these application scenarios, the common characteristics of data operations are:
(1) large amount of data
(2) frequent write operations (reading and writing are very frequent)
(3) data with low value and low transactional requirements

For such data, we are more suitable to use MongoDB to store data.

When to choose MongoDB?

In terms of architecture selection, in addition to the above three characteristics, if you still hesitate to choose it? Some of the following issues can be considered:
applications do not require transaction and complex join support.
For new applications, the requirements will change, and the data model cannot be determined. I want to develop iteratively quickly.
The application requires more than 2000-3000 read and write QPS (QPS is the query rate per second, which is a measure of the amount of traffic processed by a specific query server within a specified time.) The application requires TB or even PB
level data storage, and the application develops rapidly , it needs to be able to quickly expand horizontally, and the data stored by the application requirements will not be lost.
Applications require 99.999% high availability, and applications require a large number of geographic location queries and text queries

If one of the above matches, you can consider MongoDB. If two or more matches, you will never regret choosing MongoDB. Compared with MySQL, MongoDB can solve problems at a lower cost (including learning, development, operation and maintenance costs, etc.)

2. Introduction to MongoDB

MongoDB is an open source, high-performance, schema-free document database. It was originally designed to simplify development and facilitate expansion. It is one of the NoSQL database products. It is
a non-relational database most similar to a relational database (MySQL).

The data structure it supports is very loose. It is a JSON-like format called BSON, so it can store more complex data types and is quite flexible.

A record in MongoDB is a document, which is a data structure consisting of field and value pairs (field:value). MongoDB documents are similar to JSON objects, that is, a document
is considered an object. The data type of the field is a character type, and its value can include other documents, ordinary arrays, and document arrays in addition to some basic types.

3. Architecture

Comparison between MySQL and MongoDB:

SQL terms/concepts MongoDB terms/concepts explain
database database database
table collection Database Table/Collection
row document Data record line/document
column field data fields/fields
index index index
table joins no correspondence Table join, MongoDB does not support
no correspondence embedded document MongoDB replaces multi-table joins with embedded documents
primary key primary key Primary key, MongoDB automatically sets the _id field as the primary key

insert image description here

4. Data Model

The smallest storage unit of MongoDB is the document object. A document object corresponds to a row in a relational database. Data is stored on disk in MongoDB as
BSON (Binary-JSON) documents.

BSON (Binary Serialized Document Format) is a json-like binary storage format, referred to as Binary JSON. Like JSON, BSON supports embedded document objects and array objects, but BSON has some data types that JSON does not, such as Date and BinData types.

BSON adopts the name and pair representation method similar to the C language structure, supports embedded document objects and array objects, has three characteristics of lightness, traversability, and high efficiency, and can effectively describe unstructured data and structured data. The advantage of this format is high flexibility, but its disadvantage is that the space utilization rate is not very ideal.

In Bson, in addition to the basic JSON types: string, integer, boolean, double, null, array and object, mongo also uses special data types. These types include date, object id, binary data, regular expression and code. Each driver implements these types in a language-specific way, see your driver's documentation for details.

BSON data type reference list:

type of data describe example
string UTF-8 strings can be represented as string type data {“x” : “foobar”}
object id The object id is the 12-byte unique ID of the document {“X” :ObjectId() }
Boolean value true or false: true or false {“x”:true}+
array A collection or list of values ​​can be represented as an array {“x” : [“a”, “b”, “c”]}
32 bit integer Type not available. JavaScript only supports 64-bit floating point numbers, so 32-bit integers are automatically converted. The shell does not support this type, and the shell will convert it to a 64-bit floating point number by default
64-bit integer This type is not supported. The shell uses a special embedded document to display 64-bit integers The shell does not support this type, and the shell will convert it to a 64-bit floating point number by default
64-bit float The numbers in the shell are of this type {“x”:3.14159,“y”:3}
null Represents a null or undefined object {“x”:null}
undefined Undefined types can also be used in the document {“x”:undefined}
symbol The shell does not support it, and the shell will automatically convert the symbolic data in the database into a string
regular expression Documents can contain regular expressions, using JavaScript's regular expression syntax {“x” : /foobar/i}
JavaScript code Documents can also contain JavaScript code {“x” : function() { /* …… */ }}
binary data Binary data can consist of any string of bytes, but cannot be used in the shell
maximum value minimum value BSON includes a special type representing the maximum possible value. There is no such type in the shell.
date A BSON Date is a 64-bit integer representing the number of milliseconds since the Unix epoch (January 1, 1970). This results in a representable date range of about 290 million years in the past and future. {“x” : new Date()}
timestamp BSON timestamp type for internal MongoDB use. In most cases, in application development, you will need to use the BSON date type

Tip : The shell uses 64-bit floating-point values ​​by default. {"x": 3.14} or {"x": 3}. For integer values, you can use NumberInt (4-byte signed integer) or NumberLong (8-byte signed integer), such as:{“x”:NumberInt(3)}{“x”:NumberLong(3)}

5. Features of MongoDB

MongoDB mainly has the following characteristics:

(1) High-performance
MongoDB provides high-performance data persistence, especially support for embedded data models reduces I/O activities on the database system.

Indexes enable faster queries and can include keys from embedded documents and arrays. (Text index solves the needs of search, TTL index solves the need for automatic expiration of historical data, geographic location index can be used to build various O2O applications)

Mmapv1, wiredtiger, mongorocks (rocksdb), in-memory and other multi-engine supports meet the needs of various scenarios, and Gridfs solves the needs of file storage.

(2) The high-availability
MongoDB replication tool is called a replica set (replica set), which provides automatic failover and data redundancy.

(3) High scalability
MongoDB provides horizontal scalability as part of its core functionality.

Sharding distributes data across a cluster of machines. (Massive data storage, horizontal expansion of service capabilities)

Starting with 3.4, MongoDB supports creating data regions based on shard keys. In a balanced cluster, MongoDB directs reads and writes covered by a region only to those shards within that region.

(4) Rich query support
MongoDB supports a rich query language, supports read and write operations (CRUD), such as data aggregation, text search and geospatial query.

2. MongoDB stand-alone deployment

1. Install and start MongoDB in Windows system

(1) Step 1: Download the installation package

MongoDB provides precompiled binary packages for 32-bit and 64-bit systems. You can download and install them from the official website of MongoDB. MongoDB precompiled binary packages download address: https://www.mongodb.com/try/download/community According to the
insert image description here
above Download the zip package as shown.

Version selection: MongoDB version naming convention such as: xyz;
when y is an odd number, it means that the current version is a development version, such as: 1.5.2, 4.1.13;
when y is an even number, it means that the current version is a stable version, such as: 1.6.3 , 4.0.10;
z is the revised version number, the bigger the number, the better.

(2) Step 2: Unzip the installation and start
Unzip the compressed package into a directory. In the decompression directory, manually create a directory for storing data files, such asdata/db

Method 1: Start the service with command line parameters
Open the command line prompt in the bin directory and enter the following command:

.\mongod.exe --dbpath=..\data\db

insert image description here
We can see in the startup information that the default port of MongoDB is 27017. If we want to change the default startup port, we can specify the port through –port.
In order to facilitate our startup every time, we can set the bin directory of the installation directory to the path of the environment variable. The bin directory contains some common commands, such as mongod startup service and mongo client connection service.

Method 2: Start the service in configuration file mode (the configuration file method is recommended for deployment).
Create a new config folder in the decompression directory, and create a new configuration file mongod.conf in this folder. The contents are as follows:

storage:
   #The directory where the mongod instance stores its data.Default Value is "\data\db" on Windows.
   dbPath: D:\02_Server\DBServer\mongodb-win32-x86_64-2008plus-ssl-4.0.1\data

More parameter configuration:

systemLog:
   destination: file
   #The path of the log file to which mongod or mongos should send all diagnostic logging information
   path: "D:/02_Server/DBServer/mongodb-win32-x86_64-2008plus-ssl-4.0.1/log/mongod.log"
   logAppend: true
storage:
   journal:
	   enabled: true
   #The directory where the mongod instance stores its data.Default Value is "/data/db".
   dbPath: "D:/02_Server/DBServer/mongodb-win32-x86_64-2008plus-ssl-4.0.1/data"
net:
   #bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false

For detailed configuration items, please refer to the official documentation: https://docs.mongodb.com/manual/reference/configuration-options/

【Note】
(1) If double quotation marks are used in the configuration file, such as the path address, the content of the double quotation marks will be escaped automatically. If not escaped, an error will be reported:
error-parsing-yaml-config-file-yaml-cpp-error-at-line-3-column-15-unknown-escape-character-d

Solution: a. Replace \ with / or \ b. If there is no space in the path, quotes are not required.

(2) Tabs cannot be used to separate fields in the configuration file
Solution: convert it into a space.

To configure the startup mode of the file, run the following command in the bin directory:

.\mongod.exe -f ../config/mongod.conf

or

.\mongod.exe --config ../config/mongod.conf

(3) Download and install mongo shell
mongo shell is the interactive JavaScript Shell interface of MongoDB, which provides a powerful interface for system administrators and a way for developers to test queries and operations directly through the database. mongo also provides a full-featured JavaScript environment for MongoDB.

The MongoDB server version 6.0.2 does not provide Mongo Shell, so you need to download and install it yourself.

You can click this link to install the compressed package of mongo shell: https://www.mongodb.com/try/download/shellinsert image description here

2. Shell connection (mongo command)

Enter the bin directory, create a new cmd window (the cmd window that started the service before cannot be closed), and enter the following shell command at the command prompt to complete the login:

.\mongosh.exe

or

.\mongosh.exe --host=127.0.0.1 --port=27017

View existing databases:

show databases

exit mongodb:

exit

More parameters can be viewed through help:

.\mongosh.ext --help

Tip: MongoDB javascript shell is a javascript-based interpreter, so it supports js programs.

3. Compass- GUI client

Go to the MongoDB official website to download MongoDB Compass, the download address is: https://www.mongodb.com/try/download/compass

insert image description here
If it is the downloaded installation version, follow the steps to install it; if it is the downloaded and compressed version, decompress it directly and execute the MongoDBCompassCommunity.exe file inside, it does not matter which installation method you use.

In the opened interface, enter the host address, port and other relevant information, and click Connect (it will help us to fill in the default, just click Connect):
insert image description hereinsert image description here

3. Installation, startup and connection in Linux system

Goal: Deploy a single-machine MongoDB in Linux for use in a production environment.

1. Download the compressed package of the corresponding version

First check the Linux distribution version of the Linux server, and run the following command to query:

cat /etc/redhat-release 

insert image description here

Then go to the official website to download the tgz compressed package corresponding to the Linux version. The download address is: https://www.mongodb.com/try/download/community

insert image description here

2. Upload and decompress the compressed package

(1) Upload compressed package:
You can upload the downloaded compressed package file to the Linux server in your own way. I recommend Xshell and Xftp to upload here. Friends who have not installed Xshell and Xftp can refer to my previous [Linux Remote Management] Xshell and Xftp installation and use this blog.

(2) Unzip to the current directory:

tar -xvf mongodb-linux-x86_64-rhel70-6.0.2.tgz

3. Configure and start the service

(1) Move the decompressed folder to the specified directory

mv mongodb-linux-x86_64-rhel70-6.0.2 /usr/local/mongodb

(2) Create several new directories to store data and logs respectively

#数据存储目录
mkdir -p /mongodb/single/data/db
#日志存储目录
mkdir -p /mongodb/single/log

(3) Create and modify configuration files

vi /mongodb/single/mongod.conf

The content of the configuration file is as follows:

systemLog:
   #MongoDB发送所有日志输出的目标指定为文件
   # #The path of the log file to which mongod or mongos should send all diagnostic logging information
   destination: file
   #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
   path: "/mongodb/single/log/mongod.log"
   #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。
   logAppend: true
storage:
   #mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
   ##The directory where the mongod instance stores its data.Default Value is "/data/db".
   dbPath: "/mongodb/single/data/db"
   journal:
      #启用或禁用持久性日志以确保数据文件保持有效和可恢复。
      enabled: true
processManagement:
   #启用在后台运行mongos或mongod进程的守护进程模式。
   fork: true
net:
   #服务实例绑定的IP,默认是localhost(如果要改也应该改为服务器的局域网IP 如eth0,而不是公网IP。)
   bindIp: localhost
   #bindIp
   #绑定的端口,默认是27017
   port: 27017

(4) Start the MongoDB service

/usr/local/mongodb/bin/mongod -f /mongodb/single/mongod.conf

insert image description here

Note : If it is not successfully started, it means that the startup failed. First check the port to see if the security group has opened the port 27017. The second is whether the configuration file is wrong.

Check whether the service is started by process:

ps -ef |grep mongod

insert image description here

4. Connection test

Run the mongo command to test it:

mongo

insert image description here

Use the compass tool on Windows to test the connection, and change the host name to the public network IP connection.

Stop shutting down the service:

There are two ways to stop the service: fast shutdown and standard shutdown, which are described in turn below:

(1) Quick shutdown method (quick, simple, and data may be wrong)
Goal: Kill all mongodb processes directly through the system's kill command:
check after killing to avoid not killing some.

If it is due to data corruption, you need to do the following (understand):
(1) Delete the lock file:

rm -f /mongodb/single/data/db/*.lock

(2) Repair data:

/usr/local/mongdb/bin/mongod --repair --dbpath=/mongodb/single/data/db

(2) Standard shutdown method (data is not easy to make mistakes):
Objective: to shut down the service through the shutdownServer command in the mongo client.
The main operation steps are as follows:

//客户端登录服务,注意,这里通过localhost登录,如果需要远程登录,必须先登录认证才行。
mongo --port 27017
//#切换到admin库
use admin
//关闭服务
db.shutdownServer()

4. Basic common commands of MongoDB

The following will learn how to use common commands of MongoDB in the form of a case.

1. Case requirements

The data for storing article comments is stored in MongoDB. The data structure reference is as follows:
database name: articledb
database table (collection): column article comment comment

The fields are as follows:

Field Name field meaning Field Type Backup
_id ID primary key ObjectId或String The field of the primary key automatically generated by Mongo
articleid Article ID String
content comments String
userid Reviewer ID String
nickname Reviewer Nickname String
createdatetime datetime of the comment Date
likenum Likes Int32
replynum Replies Int32
state state String 0: invisible; 1: visible;
parentid Superior ID String If 0 means the article's top comment

2. Commands related to database operations

(1) Select and create a database
Syntax for selecting and creating a database:

use 数据库名称

Automatically create the database if it does not exist:

use articledb

View all database commands that you have permission to view:

show dbs

or

show databases

Note: In MongoDB, the collection will only be created after the content is inserted! That is to say, after the collection (data table) is created, a document (record) must be inserted before the collection is actually created. Although not shown at this time, the database has indeed been created in memory.

Command to view the database currently in use:

db

The default database in MongoDB is test, if you do not select a database, the collection will be stored in the test database.

Also:
The database name can be any UTF-8 string that satisfies the following conditions:

  • Cannot be an empty string ("").
  • Must not contain ' ' (space), ., $, /, \, and \0 (null character).
  • Should be all lowercase.
  • Up to 64 bytes.

有一些数据库名是保留的,可以直接访问这些有特殊作用的数据库:

  • admin: 从权限的角度来看,这是"root"数据库。要是将一个用户添加到这个数据库,这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行,比如列出所有的数据库或者关闭服务器。
  • local: 这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合(以后部署集群时用到)
  • config: 当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息。

(2) 数据库的删除

MongoDB 删除数据库的语法格式如下,我们在当前的数据库下运行如下命令:

db.dropDatabase()

提示:主要用来删除已经持久化的数据库

3. 集合操作相关的命令

集合,类似关系型数据库中的表。可以显示的创建,也可以隐式的创建。

(1) 集合的显式创建(了解)
基本语法格式:

db.createCollection(name)

参数说明:
name: 要创建的集合名称

例如:创建一个名为 mycollection 的普通集合。

db.createCollection("comment")

查看当前库中的表:show tables命令:

show collections

show tables

集合的命名规范:

  • 集合名不能是空字符串""。
  • 集合名不能含有\0字符(空字符),这个字符表示集合名的结尾。
  • 集合名不能以system.开头,这是为系统集合保留的前缀。
  • 用户创建的集合名字不能含有保留字符。有些驱动程序的确支持在集合名里面包含,这是因为某些系统生成的集合中包含该字符。除非你要访问这种系统创建的集合,否则千万不要在名字里出现$等保留字。

(2) 集合的隐式创建(主要创建方式)

当向一个集合中插入一个文档的时候,如果集合不存在,则会自动创建集合。所以关于集合的隐式创建,会在本文后面小节详细讲解。

提示:通常我们使用隐式创建文档即可。

(3) 集合的删除

集合删除语法格式如下:

db.collection.drop()

db.集合.drop()

返回值:如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。
例如:要删除刚刚创建的comment集合

db.comment.drop()

4. 文档基本CRUD的命令

文档(document)的数据结构和 JSON 基本一样。所有存储在集合中的数据都是 BSON 格式。

(1) 文档的插入

① 单个文档插入
使用insert() 或 save() 方法向集合中插入文档,语法如下:

db.collection.insert(
	<documents>,
	{
    
    
	  writeConcern: <document>,
	  ordered: <boolean>
	}
)

参数说明:

参数 参数类型 解释
document document 要插入到集合中的文档或文档数组。((json格式)
writeConcern document 性能和可靠性的一个级别
ordered boolean 可选。如果为真,则按顺序插入数组中的文档,如果其中一个文档出现错误,MongoDB将返回而不处理数组中的其余文档。如果为假,则执行无序插入,如果其中一个文档出现错误,则继续处理数组中的主文档。在版本2.6+中默认为true

主要用到第一个参数。

【示例】
要向comment的集合(表)中插入一条测试数据:

db.comment.insert({
    
    "articleid":"100000","content":"今天天气真好,阳光明媚","userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null})

提示:
► comment集合如果不存在,则会隐式创建
► mongo中的数字,默认情况下是double类型,如果要存整型,必须使用函数NumberInt(整型数字),否则取出来就有问题了。
► 插入当前日期使用 new Date()
► 插入的数据没有指定 _id ,会自动生成主键值
► 如果某字段没值,可以赋值为null,或不写该字段。

执行后,如下,则说明插入一个数据成功了。
insert image description here
我们在Compass可视化工具里也能看到新增的这一条文档:
insert image description here

注意:
⧪ 文档中的键/值对是有序的。
⧪ 文档中的值不仅可以是在双引号里面的字符串,还可以是其他几种数据类型(甚至可以是整个嵌入的文档)。
⧪ MongoDB区分类型和大小写。
⧪ MongoDB的文档不能有重复的键。
⧪ 文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。

文档键命名规范:
⧫ 键不能含有\0 (空字符)。这个字符用来表示键的结尾。
.$有特别的意义,只有在特定环境下才能使用。
⧫ 以下划线_开头的键是保留的(不是严格要求的)。

② 批量插入
语法:

db.collection.insertMany(
	[ <document 1> , <document 2>, ... ],
	{
    
    
	  writeConcern: <document>,
	  ordered: <boolean>
	}
)

【示例】
批量插入多条文章评论:

db.comment.insertMany([
	{
    
    "_id":"1","articleid":"100001","content":"我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我
他。","userid":"1002","nickname":"相忘于江湖","createdatetime":new Date("2019-08-
05T22:08:15.522Z"),"likenum":NumberInt(1000),"state":"1"},
	{
    
    "_id":"2","articleid":"100001","content":"我夏天空腹喝凉开水,冬天喝温开水","userid":"1005","nickname":"伊人憔","createdatetime":new Date("2019-08-05T23:58:51.485Z"),"likenum":NumberInt(888),"state":"1"},
	{
    
    "_id":"3","articleid":"100001","content":"我一直喝凉开水,冬天夏天都喝。","userid":"1004","nickname":"杰克船","createdatetime":new Date("2019-08-06T01:05:06.321Z"),"likenum":NumberInt(666),"state":"1"},
	{
    
    "_id":"4","articleid":"100001","content":"专家说不能空腹吃饭,影响健康。","userid":"1003","nickname":"凯","createdatetime":new Date("2019-08-06T08:18:35.288Z"),"likenum":NumberInt(2000),"state":"1"},
	{
    
    "_id":"5","articleid":"100001","content":"研究表明,刚烧开的水千万不能喝,因为烫
嘴。","userid":"1003","nickname":"凯撒","createdatetime":new Date("2019-08-
	06T11:01:02.521Z"),"likenum":NumberInt(3000),"state":"1"}
]);

提示:

插入时指定了 _id ,则主键就是该值。

如果某条数据插入失败,将会终止插入,但已经插入成功的数据不会回滚掉。

因为批量插入由于数据较多容易出现失败,因此,可以使用try catch进行异常捕捉处理,测试的时候可以不处理。如(了解):

try {
    
    
db.comment.insertMany([
	{
    
    "_id":"1","articleid":"100001","content":"我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我
他。","userid":"1002","nickname":"相忘于江湖","createdatetime":new Date("2019-08-
05T22:08:15.522Z"),"likenum":NumberInt(1000),"state":"1"},
	{
    
    "_id":"2","articleid":"100001","content":"我夏天空腹喝凉开水,冬天喝温开水","userid":"1005","nickname":"伊人憔","createdatetime":new Date("2019-08-05T23:58:51.485Z"),"likenum":NumberInt(888),"state":"1"},
	{
    
    "_id":"3","articleid":"100001","content":"我一直喝凉开水,冬天夏天都喝。","userid":"1004","nickname":"杰克船","createdatetime":new Date("2019-08-06T01:05:06.321Z"),"likenum":NumberInt(666),"state":"1"},
	{
    
    "_id":"4","articleid":"100001","content":"专家说不能空腹吃饭,影响健康。","userid":"1003","nickname":"凯","createdatetime":new Date("2019-08-06T08:18:35.288Z"),"likenum":NumberInt(2000),"state":"1"},
	{
    
    "_id":"5","articleid":"100001","content":"研究表明,刚烧开的水千万不能喝,因为烫
嘴。","userid":"1003","nickname":"凯撒","createdatetime":new Date("2019-08-
06T11:01:02.521Z"),"likenum":NumberInt(3000),"state":"1"}
]);
} catch (e) {
    
    
print (e);
}

5. 查询文档的基本命令

查询数据的语法格式如下:

db.collection.find(<query>, [projection])

参数:

参数 参数类型 解释
query document 可选。使用查询运算符指定选择筛选器。若要返回集合中的所有文档,请省略此参数或传递空文档( {} )。
projection document 可选。指定要在与查询筛选器匹配的文档中返回的字段(投影)。若要返回匹配文档中的所有字段,请省略此参数。

【示例】

① 查询所有
如果我们要查询集合的所有文档,我们输入以下命令:

db.comment.find()

db.comment.find({
    
    })

这里你会发现每条文档会有一个叫_id的字段,这个相当于我们原来关系数据库中表的主键,当你在插入文档记录时没有指定该字段,
MongoDB会自动创建,其类型是ObjectID类型。

如果我们在插入文档记录时指定该字段也可以,其类型可以是ObjectID类型,也可以是MongoDB支持的任意类型。

如果我想按一定条件来查询,比如我想查询userid为1003的记录,怎么办?很简单!只 要在find()中添加参数即可,参数也是json格式,如下:

db.comment.find({
    
    userid:'1003'})

如果你只需要返回符合条件的第一条数据,我们可以使用findOne命令来实现,语法和find一样。

如:查询用户编号是1003的记录,但只最多返回符合条件的第一条记录:

db.comment.findOne({
    
    userid:'1003'})

② 投影查询(Projection Query):

如果要查询结果返回部分字段,则需要使用投影查询(不显示所有字段,只显示指定的字段)。

如:查询结果只显示 _id、userid、nickname :

db.comment.find({
    
    userid:"1003"},{
    
    userid:1,nickname:1})

insert image description here

我们发现, _id 默认会显示。

查询结果只显示 、userid、nickname ,不显示 _id :

db.comment.find({
    
    userid:"1003"},{
    
    userid:1,nickname:1,_id:0})

insert image description here

查询所有数据,但只显示 _id、userid、nickname

db.comment.find({
    
    },{
    
    userid:1,nickname:1})

6. 更新文档的命令

更新文档的语法:

db.collection.update(query, update, options)
//或
db.collection.update(
<query>,
<update>,
{
    
    
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2
}

参数:

参数 参数类型 解释
query document 更新的选择条件。可以使用与find()方法中相同的查询选择器,类似sql update查询内where后面的。在3.0版中进行了更改:当使用upsert:true执行update()时,如果查询使用点表示法在_id字段上指定条件,则MongoDB将拒绝插入新文档。
update document 或者pipeline 要应用的修改。该值可以是:包含更新运算符表达式的文档,或仅包含:对的替换文档,或在MongoDB 4.2中启动聚合管道。
upsert boolean 可选。如果设置为true,则在没有与查询条件匹配的文档时创建新文档。默认值为false,如果找不到匹配项,则不会插入新文档。
multi boolean 可选。如果设置为true,则更新符合查询条件的多个文档。如果设置为false,则更新一个文档。默认值为false。
writeConcern document 可选。表示写问题的文档。抛出异常的级别。
collation document 可选。指定要用于操作的校对规则。校对规则允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音标记的规则。
arrayFilters array 可选。一个筛选文档数组,用于确定要为数组字段上的更新操作修改哪些数组元素
hint Document 或 string 可选。指定用于支持查询谓词的索引的文档或字符串。该选项可以采用索引规范文档或索引名称字符串。如果指定的索引不存在,则说明操作错误。例如,请参阅版本4中的“为更新操作指定提示。

提示:主要关注前四个参数即可。

【示例】

① 覆盖的修改

如果我们想修改_id为1的记录,点赞量为1001,输入以下语句:

db.comment.updateOne({
    
    _id:"1"},{
    
    likenum:NumberInt(1001)})

执行后,我们会发现,这条文档除了likenum字段其它字段都不见了,它用后面的json把文档数据全部替换掉了。

② 局部修改

为了解决这个问题,我们需要使用修改器$set来实现,命令如下:

我们想修改_id为2的记录,浏览量为889,输入以下语句:

db.comment.update({
    
    _id:"2"},{
    
    $set:{
    
    likenum:NumberInt(889)}})

这样就OK啦。

③ 批量的修改

更新所有用户为 1003 的用户的昵称为 凯撒大帝 。

//默认只修改第一条数据
db.comment.update({
    
    userid:"1003"},{
    
    $set:{
    
    nickname:"凯撒2"}})
//修改所有符合条件的数据
db.comment.update({
    
    userid:"1003"},{
    
    $set:{
    
    nickname:"凯撒大帝"}},{
    
    multi:true})

提示:如果不加后面的 multi参数,则也只更新符合条件的第一条记录

④ 列值增长的修改

如果我们想实现对某列值在原有值的基础上进行增加或减少,可以使用 $inc 运算符来实现。

需求:对3号数据的点赞数,每次递增1

db.comment.update({
    
    _id:"3"},{
    
    $inc:{
    
    likenum:NumberInt(1)}})

7. 删除文档的命令

删除文档的语法结构:

db.集合名称.remove(条件)

以下语句可以将数据全部删除,请慎用:

db.comment.remove({
    
    })

如果删除_id=1的记录,输入以下语句

db.comment.remove({
    
    _id:"1"})

8. 文档统计查询的命令

统计查询使用count()方法,语法如下:

db.collection.count(query, options)

参数:

参数 参数类型 解释
query document 查询选择条件。
options document 可选。用于修改计数的额外选项。

提示:可选项暂时不使用。

【示例】

① 统计所有记录数

例如:统计comment集合的所有的记录数:

db.comment.count()

② 按条件统计记录数

例如:统计userid为1003的记录条数

db.comment.count({
    
    userid:"1003"})

提示:默认情况下 count() 方法返回符合条件的全部记录条数。

9. 分页列表查询的命令

可以使用limit()方法来读取指定数量的数据,使用skip()方法来跳过指定数量的数据。

基本语法如下所示:

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

如果你想返回指定条数的记录,可以在find方法后调用limit来返回结果(TopN),默认值20,例如:

db.comment.find().limit(3)

skip() 方法同样接受一个数字参数作为跳过的记录条数。(前N个不要), 默认值是0

db.comment.find().skip(3)

分页查询:需求:每页2个,第二页开始:跳过前两条数据,接着值显示3和4条数据

//第一页
db.comment.find().skip(0).limit(2)
//第二页
db.comment.find().skip(2).limit(2)
//第三页
db.comment.find().skip(4).limit(2)

10. 文档排序查询的命令

sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。

语法如下所示:

db.COLLECTION_NAME.find().sort({
    
    KEY:1})

db.集合名称.find().sort(排序方式)

例如:对userid降序排列,并对访问量进行升序排列

db.comment.find().sort({
    
    userid:-1,likenum:1})

提示:skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是 limit(),和命令编写顺序无关。

11. 文档相关的更多查询命令

① 正则的复杂条件查询

MongoDB的模糊查询是通过正则表达式的方式实现的。格式为:

db.collection.find({
    
    field:/正则表达式/})

db.集合.find({
    
    字段:/正则表达式/})

提示:正则表达式是js的语法,直接量的写法。

例如,我要查询评论内容包含“开水”的所有文档,代码如下:

db.comment.find({
    
    content:/开水/})

如果要查询评论的内容中以“专家”开头的,代码如下:

db.comment.find({
    
    content:/^专家/})

② 比较查询

<, <=, >, >= 这个操作符也是很常用的,格式如下:

db.集合名称.find({
    
     "field" : {
    
     $gt: value }}) // 大于: field > value
db.集合名称.find({
    
     "field" : {
    
     $lt: value }}) // 小于: field < value
db.集合名称.find({
    
     "field" : {
    
     $gte: value }}) // 大于等于: field >= value
db.集合名称.find({
    
     "field" : {
    
     $lte: value }}) // 小于等于: field <= value
db.集合名称.find({
    
     "field" : {
    
     $ne: value }}) // 不等于: field != value

示例:查询评论点赞数量大于700的记录

db.comment.find({
    
    likenum:{
    
    $gt:NumberInt(700)}})

③ 包含查询

包含使用$in操作符。

示例:查询评论的集合中userid字段包含1003或1004的文档

db.comment.find({
    
    userid:{
    
    $in:["1003","1004"]}})

不包含使用$nin操作符。

示例:查询评论集合中userid字段不包含1003和1004的文档

db.comment.find({
    
    userid:{
    
    $nin:["1003","1004"]}})

④ 条件连接查询

我们如果需要查询同时满足两个以上条件,需要使用$and操作符将条件进行关联。(相 当于SQL的and) 格式为:

$and:[ {
    
     },{
    
     },{
    
     } ]

示例:查询评论集合中likenum大于等于700 并且小于2000的文档:

db.comment.find({
    
    $and:[{
    
    likenum:{
    
    $gte:NumberInt(700)}},{
    
    likenum:{
    
    $lt:NumberInt(2000)}}]})

如果两个以上条件之间是或者的关系,我们使用$or 操作符进行关联,与前面 and的使用方式相同 格式为:

$or:[ {
    
     },{
    
     },{
    
     } ]

示例:查询评论集合中userid为1003,或者点赞数小于1000的文档记录

db.comment.find({
    
    $or:[ {
    
    userid:"1003"} ,{
    
    likenum:{
    
    $lt:1000} }]})

12. 常用命令小结

// 选择切换数据库
use articledb
// 插入数据
db.comment.insert({
    
    bson数据})
// 查询所有数据
db.comment.find();
// 条件查询数据
db.comment.find({
    
    条件})
// 查询符合条件的第一条记录
db.comment.findOne({
    
    条件})
// 查询符合条件的前几条记录:
db.comment.find({
    
    条件}).limit(条数)
// 查询符合条件的跳过的记录:
db.comment.find({
    
    条件}).skip(条数)
// 修改数据
db.comment.update({
    
    条件},{
    
    修改后的数据}) 或db.comment.update({
    
    条件},{
    
    $set:{
    
    要修改部分的字段:数据})
// 修改数据并自增某字段值 
db.comment.update({
    
    条件},{
    
    $inc:{
    
    自增的字段:步进值}})
// 删除数据
db.comment.remove({
    
    条件})
// 统计查询
db.comment.count({
    
    条件})
// 模糊查询
db.comment.find({
    
    字段名:/正则表达式/})
// 条件比较运算
db.comment.find({
    
    字段名:{
    
    $gt:}})
// 包含查询
db.comment.find({
    
    字段名:{
    
    $in:[1,值2]}})或db.comment.find({
    
    字段名:{
    
    $nin:[1,值2]}})
// 条件连接查询
db.comment.find({
    
    $and:[{
    
    条件1},{
    
    条件2}]})或db.comment.find({
    
    $or:[{
    
    条件1},{
    
    条件2}]})

五. MongoDB索引—Index

1. 索引概述

索引支持在MongoDB中高效地执行查询。如果没有索引,MongoDB必须执行全集合扫描,即扫描集合中的每个文档,以选择与查询语句匹配的文档。这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对网站的性能是非
常致命的。

如果查询存在适当的索引,MongoDB可以使用该索引限制必须检查的文档数。

索引是特殊的数据结构,它以易于遍历的形式存储集合数据集的一小部分。索引存储特定字段或一组字段的值,按字段值排序。索引项的排序支持有效的相等匹配和基于范围的查询操作。此外,MongoDB还可以使用索引中的排序返回排序结果。

官网文档:https://docs.mongodb.com/manual/indexes/

MongoDB索引使用B树数据结构(确切的说是B-Tree,MySQL是B+Tree)

2. 索引的类型

(1) 单字段索引

MongoDB支持在文档的单个字段上创建用户定义的升序/降序索引,称为单字段索引(Single Field Index)。对于单个字段索引和排序操作,索引键的排序顺序(即升序或降序)并不重要,因为MongoDB可以在任何方向上遍历索引。
insert image description here

(2) 复合索引

MongoDB还支持多个字段的用户定义索引,即复合索引(Compound Index)。

复合索引中列出的字段顺序具有重要意义。例如,如果复合索引由 { userid: 1, score: -1 } 组成,则索引首先按userid正序排序,然后在每个userid的值内,再在按score倒序排序。
insert image description here

**(3) 其他索引 (了解) **

① 地理空间索引(Geospatial Index)
为了支持对地理空间坐标数据的有效查询,MongoDB提供了两种特殊的索引:返回结果时使用平面几何的二维索引和返回结果时使用球面几何的二维球面索引。

② 文本索引(Text Indexes)
MongoDB提供了一种文本索引类型,支持在集合中搜索字符串内容。这些文本索引不存储特定于语言的停止词(例如“the”、“a”、“or”),
而将集合中的词作为词干,只存储根词。

③ 哈希索引(Hashed Indexes)
为了支持基于散列的分片,MongoDB提供了散列索引类型,它对字段值的散列进行索引。这些索引在其范围内的值分布更加随机,但只支
持相等匹配,不支持基于范围的查询。

3. 索引的查看

说明:返回一个集合中的所有索引的数组。

语法:

db.collection.getIndexes()

提示:该语法命令运行要求是MongoDB 3.0+

【示例】

查看comment集合中所有的索引情况

db.comment.getIndexes()

insert image description here

结果中显示的是默认 _id 索引。v表示引擎的版本号,key指那个字段上加的索引,name说索引的名称。

默认_id索引:MongoDB在创建集合的过程中,在 _id 字段上创建一个唯一的索引,默认名字为 id ,该索引可防止客户端插入两个具有相同值的文档,您不能在_id字段上删除此索引。

注意:该索引是唯一索引,因此值不能重复,即 _id 值不能重复的。在分片集群中,通常使用 _id 作为片键。

4. 索引的创建

说明:在集合上创建索引。

语法:

db.collection.createIndex(keys, options)

参数:

参数 参数类型 解释
keys document 包含字段和值对的文档,其中字段是索引键,值描述该字段的索引类型。对于字段上的升序索引,请指定值1;对于降序索引,请指定值-1。比如: {字段:1或-1} ,其中1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可。另外,MongoDB支持几种不同的索引类型,包括文本、地理空间和哈希索引。
options document 可选。包含一组控制索引创建的选项的文档。有关详细信息,请参见选项详情列表。

options(更多选项)列表:

参数 参数类型 解释
background boolean 建索引过程会阻塞其它数据库操作,background可指定以后台方式创建索引,即增加"background" 可选参数。 “background” 默认值为false。
unique boolean 建立的索引是否唯一。指定为true创建唯一索引。默认值为false.
name string 索引的名称。如果未指定,MongoDB的通过连接索引的字段名和排序顺序生成一个索引名称。
dropDups boolean 3.0+版本已废弃。在建立唯一索引时是否删除重复记录,指定 true 创建唯一索引。默认值为false.
sparse boolean 对文档中不存在的字段数据不启用索引;这个参数需要特别注意,如果设置为true的话,在索引字段中不会查询出不包含对应字段的文档.。默认值为 false.
expireAfterSeconds integer 指定一个以秒为单位的数值,完成 TTL设定,设定集合的生存时间。
v index version 索引的版本号。默认的索引版本取决于mongod创建索引时运行的版本。
weights document 索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重。
default_language string 对于文本索引,该参数决定了停用词及词干和词器的规则的列表。 默认为英语
language_override string 对于文本索引,该参数指定了包含在文档中的字段名,语言覆盖默认的language,默认值为language.

注意 : 在 3.0.0 版本前创建索引方法为 db.collection.ensureIndex() ,之后的版本使用了 db.collection.createIndex() 方法,
ensureIndex() 还能用,但只是 createIndex() 的别名。

【示例】

(1) 单字段索引

示例:对 userid 字段建立索引:

db.comment.createIndex({
    
    userid:1})

(2) 复合索引

示例:对 userid 和 nickname 同时建立复合(Compound)索引:

db.comment.createIndex({
    
    userid:1,nickname:-1})

5. 索引的移除

说明:可以移除指定的索引,或移除所有索引

(1) 指定索引的移除

语法:

db.collection.dropIndex(index)

参数:

参数 参数类型 解释
index string 或 document 指定要删除的索引。可以通过索引名称或索引规范文档指定索引。若要删除文本索引,请指定索引名称。

【示例】

删除 comment 集合中 userid 字段上的升序索引:

db.comment.dropIndex({
    
    userid:1})

(2) 所有索引的移除

语法:

db.collection.dropIndexes()

【示例】

删除 comment 集合中所有索引。

db.comment.dropIndexes()

提示: _id 的字段的索引是无法删除的,只能删除非 _id 字段的索引。

6. 索引的使用

(1) 执行计划

分析查询性能(Analyze Query Performance)通常使用执行计划(解释计划、Explain Plan)来查看查询的情况,如查询耗费的时间、是
否基于索引查询等。

那么,通常,我们想知道,建立的索引是否有效,效果如何,都需要通过执行计划查看。

语法:

db.collection.find(query,options).explain(options)

【示例】

View the situation of querying data according to userid:

db.comment.find({
    
    userid:"1003"}).explain()

insert image description here
The key point: stage : 'FETCH' , indicates that the index is used.

(2) Covered Queries Covered Queries (Understanding)

When the query criteria and the query's projection contain only indexed fields, MongoDB returns results directly from the index without scanning any documents or bringing documents into memory. These covered queries can be
very efficient.

insert image description here
For example, there happens to be a score field in my index, and my projection query only checks the score field, and it can directly get data from the index.


End of the article : There are already 25,000 words here, and the necessary knowledge points of MongoDB will be sorted out here for the time being. I will continue to organize the advanced parts of MongoDB when I have time in the future. Finally, ask for a follow, like, favorite. Please, this is really important to me!


Guess you like

Origin blog.csdn.net/weixin_50216991/article/details/127176763