My name is mongodb. I don’t know my story. After reading my story, you can get started!

Time:2021-1-14

My name is mongodb. I don't know my story. After reading my story, you can get started!
This is Mongo basic, and will be updated four times in a row

Hello everyone, my name is mongodb. Since October 2007, 10gen team brought me to this world, I’m over 13 years old. Now more and more little friends are hugging me. I’m very happy. I’m a member of the NoSQL family. I’m the son of C + +. In order to be familiar with me better, today I will introduce myself from a simple point of view.

First of all, let’s introduce our big family NoSQL

NoSQL is just his abbreviation. His Chinese name is non relational database, and his foreign name is not only SQL. It’s a complement to relational databases (RDBMS). RDBMS pursues highly structured and strict data consistency in data storage and query; NoSQL does not care about form, but focuses on efficiency. NoSQL is a high-performance, modeless and highly scalable distributed database. NoSQL storage includes four types: key value pair storage, column storage, document storage and graphic data storage. NoSQL is just a brief introduction to this. This is not the focus of today. Let’s focus on my own (mongodb), ha ha!

MongoDbintroduce oneself to

Mongodb is a key value pair NoSQL database for document storage. It is a non relational database closest to relational database. Bjosn (similar to JSON) structure is used in data storage structure and query. Bjosn supports multi-layer organization. In the specific use process, it is more like operating JavaScript script. Because of the modeless mongodb, it becomes more flexible and easy to expand in practical application. Like RDBMS, it supports primary key, index, retrieval and other operations, but it doesn’t support join query because it is unstructured.

MongoDbThe basic concept of

Specific database installation is not introduced, online search a big push. Mongodb uses bjon based document storage, so its basic structure concept can be associated with JSON: field, document, set and database, which is one-to-one correspondence with RDBMS attributes, columns, tables and databases. Here is a table for comparison:

My name is mongodb. I don't know my story. After reading my story, you can get started!

A simple table display is not intuitive enough. Let’s take an image to illustrate it!

My name is mongodb. I don't know my story. After reading my story, you can get started!
My name is mongodb. I don't know my story. After reading my story, you can get started!

User rights management

In the database management of generation environment, database permission is a very important function. In terms of specific permissions, we need to control the global permissions, and we need to be precise to the specific data permissions. In terms of specific permissions classification, we need to include: read, read and write, administrator and other permissions. The following list describes mongodb’s built-in permissions:

My name is mongodb. I don't know my story. After reading my story, you can get started!

From the above table, we can draw a conclusion: according to the scope of permissions, permissions are divided into two categories: permissions for each database and permissions for all databases. According to different permission classification, there are some differences in the assignment format. In the specific user permission assignment, a user can assign multiple permissions at the same time.

When adding a new user, the format of permission initialization is as follows:

Initialization command format for all database permissions:

db.createUser ({user: “user name”, PWD: “password”, roles: [permission value]})

Initialize command format for specified database permissions:

db.createUser ({user: “user name”, PWD: “password”, roles: [{role: “permission value”, DB: “corresponding database”}, {role: “permission value”, DB: “corresponding database”} .]})

This may not be very easy to understand, so let’s take the actual use of the work as an example.

Conditional hypothesis: there are three databases: testdb001 and testdb002

Users need to be assigned corresponding permissions

My name is mongodb. I don't know my story. After reading my story, you can get started!

Through practical examples, we should have an understanding of the new initialization operation command of permissions. Now let’s have a brief talk about permissions according to the new operation command. In fact, the specific permissions format of root system and initialization is one to one, as follows:

Update command format for all database permissions:

db.updateUser (“updated user name”, {PWD: “updated password”, roles: [permission value]})

Update command format for specified database permissions:

db.updateUser (“updated user name”, {PWD: “updated password”, roles: [{role: “permission value”, DB: “corresponding database”}, {role: “permission value”, DB: “corresponding database”}) .]})

Note: the second parameter of updateuser has two nodes: PWD and roles. If you don’t need to update the data of a node, you can directly do without the node.

Instance: continue with the above instance

My name is mongodb. I don't know my story. After reading my story, you can get started!

Database

The specific database concept is consistent with RDBMS. A mongodb can create multiple databases, and different databases can also be stored in different mongodbs. The default database of the system includes: admin (store user permission related), config (configuration information), local (log information). For the database, our common operations include: create, delete. The specific implementation is as follows:

Create: use database name

When the database name doesn’t exist, the system will create it automatically (it can’t be displayed after creation, it will be displayed only after inserting data into it), and switch if it exists.

Delete: first switch to the database for, and then execute the dropDatabase command

Use the name of the deleted database

db.dropDatabase()

Collection

A set is a grouping of data storage in mongodb, which corresponds to a table in a relational database. The document data stored in the collection has no fixed format, so we can store bjson data in different formats freely, but in practice, we still store the same type of bjson data. The common operation commands of collection are as follows:

To create a collection:

db.createCollection (set name, set rule), in which the second parameter is a JSON data, which is not required. The specific parameter node is:

Set rule: {capped: optional bool type: set whether to change the set to a fixed set,

True: represents a fixed set. The data in the set cannot be modified. It is paired with size. It means that when the set reaches the specified size, the historical data (the first added data) will be automatically covered,

Size: optional number type: Specifies the maximum storage data (bytes) of the set. When the set reaches the specified size, it will automatically cover the historical data (the first added data)}

Max: optional number type: Specifies the maximum total number of documents stored in the collection. When the number of documents is greater than max, the historical documents will be replaced automatically

}

Collection delete:

DB. Set name. Drop();

Document

A document is a set of key value vs. data (a bjon). A specific document structure can be nested in multiple layers. The data structure between different documents can be different, and the data type of the same node can be different. This is the biggest difference from RDBMS, which also lays the foundation for the high scalability of mongodb. In fact, it is simply data in Jon format one by one.

Summary of several common operation commands in the document:

My name is mongodb. I don't know my story. After reading my story, you can get started!

field

Fields are easy to understand, that is, every node of the JSON data in the document.

My name is mongodb. I don't know my story. After reading my story, you can get started!

Through a brief introduction of mongodb operation, it is not difficult to find the following features:

1. All the parameters of document addition, deletion, modification and query are JSON. In actual operation, you can operate according to the JSON mode.

2. Databases and collections can be created automatically when they are used

2.1 when switching databases, if there is no database, it will be created automatically;

2.2 dB. Set. Insert() when the set does not exist, the system automatically creates the set.

Are these features of mongodb really cool to use. Next, from the creation of the database to the overall operation process of the document, write some demonstration examples to deepen the impression.

My name is mongodb. I don't know my story. After reading my story, you can get started!

Through the above operation, we find that all new documents will automatically generate a node_ “Objectid”, the_ ID is a similar unique primary key automatically generated by mongodb system, which can be generated and sorted quickly. It contains 12 bytes

1. The first four bytes indicate creationunixTime stamp, Greenwich mean timeUTCTime, 8 hours later than Beijing time

2. The next three bytes are the machine identification code

3. The next two bytes consist of process ID

4. The last three bytes are random numbers

My name is mongodb. I don't know my story. After reading my story, you can get started!

Of course_ ID can also be assigned according to actual needs.

Well, let’s write about it today. Through this article, we have a preliminary understanding of Mongo. In the next article, we will talk about the query together in detail. There are still many conversations about Mongo’s query. Thank you for checking.
END