The basic concepts in MongoDB are databases, collections, and documents .
The following table will help you understand some of the concepts in MONGO more easily:
SQL Terminology/Concepts |
Explanation/Description |
MongoDB Terminology/Concepts |
Explanation/Description |
Database |
Database |
Database |
Database |
Table |
Table |
Collection |
Collection |
row |
record lines |
document |
document |
Column |
Field |
Field |
Domain |
Index |
Index |
Index |
Index |
Table joins |
Table Connection |
|
|
Primary key |
Primary key |
Primary key |
Primary key, MongoDB automatically sets the _id field as the primary key |
Database
The concept of a database in MongoDB is basically the same as the concept of a database in a relational database. A database in MongoDB is a combination of multiple collections. In the same MongoDB, you can build multiple databases that are independent of each other and can be authenticated independently.
mongodb Reserved Database: admin, local, config
Admin database, which is a root database in which users are added, and the user inherits all permissions from the database.
Local database, this database is not replicated, only the database that is accessed by the on-premises server is stored.
the config database is used to hold information about the Shard.
Collection
A collection is a combination of a set of documents. If a document is likened to a row in a database, then the collection can be likened to a database table.
The collection exists in the database, the collection does not have a fixed structure, which means that you can insert data in different formats and types on the collection, but usually we have some relevance to the data that we insert into the collection.
For example, we can insert a document of the following different data structures into the collection:
{"Site": "www.baidu.com"}//when the first document is inserted, the collection is created.
{"Site": "www.google.com", "name": "Google"}
{"Site": "www.runoob.com", "Name": "Tutorial", "num": 5}
Valid set name
The collection name cannot be an empty string "".
The collection name cannot contain the \ s character (null character), which represents the end of the collection name.
The collection name cannot be "system." Begins with a prefix reserved for the system collection.
User-created collection names cannot contain reserved characters. Some drivers do support inclusion in the collection name, because some system-generated collections contain that character. Never show $ in a name unless you want to access a collection created by this system.
Document
The document is a key-value (Key-value) pair (that is, Bson). MongoDB documents do not need to set the same field, and the same field does not require the same data type, which is very different from the relational database, is also a very prominent MongoDB features.
It is important to note that:
The key/value pairs in the document are ordered.
the values in the document can be not only strings in double quotes, but also several other data types (even the entire embedded document).
MongoDB distinguishes between type and case.
MongoDB documents cannot have duplicate keys.
the key of the document is a string. In addition to a few exceptions, keys can use any UTF-8 character.
document key naming specification:
The key cannot contain a/s (null character). This character is used to denote the end of a key.
. and $ have special meaning and can only be used in certain circumstances.
keys that begin with the underscore "_" are reserved (not strictly required).
Attached: MongoDB data type
The following table is a few of the data types commonly used in MongoDB.
Data Type |
Description |
String |
String. Data types commonly used to store data. In MongoDB, the UTF-8 encoded string is legal. |
Integer |
Integer value. Used to store numeric values. Depending on the server you are using, it can be divided into 32-bit or 64-bit. |
Boolean |
Boolean value. Used to store Boolean values (True/false). |
Double |
Double-precision floating-point value. Used to store floating-point values. |
Min/max keys |
Compares a value to the lowest and highest value of a BSON (binary JSON) element. |
Arrays |
Used to store an array or list or multiple values as a single key. |
Timestamp |
Time stamp. Record when the document was modified or added. |
Object |
Used for inline documents. |
Null |
Used to create a null value. |
Symbol |
Symbol. The data type is basically the same as the string type, but unlike it, it is typically used in languages with special symbol types. |
Date |
Date time. Use the UNIX time format to store the current date or time. You can specify your own datetime: Create a Date object and pass in the month-date information. |
Object ID |
The object ID. The ID used to create the document. |
Binary Data |
Binary data. Used to store binary data. |
Code |
The code type. Used to store JavaScript code in a document. |
Regular expression |
Regular |
MongoDB Basic Concepts-databases, collections, documents