• Before learning the operation commands of the MongoDB database, it is necessary to have a general understanding of the basic concepts in order to facilitate subsequent learning.
  • In view of the relative relationship between SQL and NoSQL, it is helpful to understand the concepts of MongoDB and SQL together. On the one hand, you can get started with MongoDB in a short time, and on the other hand, it is beneficial to deepen your memory. In the interview, you do not need to memorize concepts by rote, and you can also give a general idea according to your understanding and thinking logic. I have read many tutorials and posts along these lines, and I will continue to follow this learning model.
  • Therefore, before learning MongoDB, it is best to spend a week to learn relational database, understand the basic concepts of SQL and simple add, delete, change and check statements, and then start MongoDB, I believe that the two databases will have a deeper understanding. I will also be adding the MySQL review notes later, as well, interested people can pay attention to the < ~ ~ /

1 Database

The database concept of MongoDB is basically the same as THAT of SQL. A single instance can hold multiple independent databases, each of which has its own set and permissions. To display all databases, run the show DBS command.

> show dbs
admin        0.000GB
config       0.000GB
local        0.000GB
Copy the code

There are three default databases, each with its own specific function.

  • Admin: From a permissions perspective, this is the root database. If you add a user to the database, the user automatically inherits all database permissions, and certain server-side commands can only be run from the database.
  • Local: This data is never replicated and can be used to store any collection limited to a single local server.
  • Config: When Mongo is used for sharding Settings, the Config database is used internally to hold information about sharding.

2 Collection

A set is a MongoDB document group, similar to a table in a relational database. The set has no fixed structure, and data of different formats and types can be inserted into the set, but usually the data inserted into the set will have certain correlation.

3 the metadata

Metadata, I personally understand it as a special collection, but use the system namespace: dbname.system.*, which includes namespace, index, profile, user and server information and status.

Copy the code

4. Document

A document is a set of key-value pairs, similar to Json. MongoDB documents do not need to set the same fields, and the same fields do not need the same data type, but generally the data inserted into the collection will have some correlation. This is one of the important differences between SQL and NoSQL.

4.1 Take an example

Create a database named socialmeida use socialmeida, then create a collection named Douyin, and insert 4 documents into it:

Db.douyin. Insert ({"aweme_id":"115615646865","aweme_name":" aweme_name","likes":555,"follower":3}) Db.douyin. Insert ({"aweme_id":11561564565,"aweme_name":"105 ° love ","follower":24}) Db.douyin. Insert ({"aweme_name":" three sentences make a man spend 18W for me","follower":" ABC "}) db.douyin. Insert ({"aweme_name":" likes":666})Copy the code

Then use Navicat to look at the data in the collection:

Doesn’t the JSON-like form look comfortable? It doesn’t matter, first to read the form of excessive ~

You can find that multiple data types can appear in the same field. Can SQL do this? Jennifer

4.2 the ObjectId

If you observe carefully, you can find that there is an extra _id field in the set without any reason. The data type of this _id field is ObjectId. It is similar to the unique primary key in SQL and contains 12bytes.

  • The first four bytes represent the creation of a Unix timestamp
  • The next three bytes are the machine identifier
  • The two bytes that follow comprise the PID consisting of the process ID
  • The last three bytes are random numbers

In order to ensure uniqueness, documents stored in MongoDB must have an _ID field. The default data type is ObjectId. Since the created timestamp is stored in ObjectId, there is no need to create another timestamp field like SQL. We can use the getTimestamp function to get the creation time of the document. Let’s look up when the first document we just entered was created:

> the ObjectId (" 60 e9b1b1da4c0000b9005b88 "). GetTimestamp () the 2021-07-10 14:41:53. 000Copy the code

5 concludes

Finally, the basic concepts mentioned above are summarized in table form:

Basic concept MongoDB SQL note
The database database database Both consistent
Collection/table collection table MongoDB collections are unstructured
Document/line document row MongoDB documents store data in a JSON-like format
Only the primary key primary key primary key The MongoDB primary key _id is automatically generated and contains a timestamp