MongoDB concept analysis
No matter what database we study, we should learn the basic concepts. The basic concepts in mongodb are documents, collections, and databases. Let’s introduce them one by one below.
The following table will help you understand some concepts in Mongo easier:
SQL terms/concepts | MongoDB terms/concepts | Explanation/Explanation |
---|---|---|
database | database | Database |
table | collection | Database table/collection |
row | document | Data record row/document |
column | field | Data field/field |
index | index | Index |
table joins | Table join, MongoDB does not support | |
primary key | primary key | Primary key, MongoDB automatically sets the _id field as the primary key |
Through the example below, we can also understand some concepts in Mongo more intuitively:
Database
A mongodb Multiple databases can be created.
MongoDB's default database is "db", which is stored in the data directory.
A single instance of MongoDB can hold multiple independent databases, each with its own collection and permissions. Different databases are also placed in different files.
"show dbs" command can display a list of all data.
$ ./mongo MongoDB shell version: 3.0.6 connecting to: test > show dbs local 0.078GB test 0.078GB >
Execute the "db" command to display the current database object or collection.
$ ./mongo MongoDB shell version: 3.0.6 connecting to: test > db test >
Run the "use" command to connect to a specified database.
> use local switched to db local > db local >
In the above example command, "local" is the database you want to link to.
In the next chapter we will explain in detail the use of commands in MongoDB.
Databases are also identified by name. The database name can be any UTF-8 string that meets the following conditions.
cannot be an empty string ("").
must not contain ' ' (space), ., $, /, \ and \0 (space character).
should be all lowercase.
Maximum 64 bytes.
Some database names are reserved, and you can directly access these databases with special functions.
admin: From a permissions perspective, this is the "root" database. If a user is added to this database, the user automatically inherits all database permissions. Certain server-side commands can also only be run from this database, such as listing all databases or shutting down the server.
local:This data will never be copied and can be used to store any collection limited to a single local server
config: When Mongo is used for sharding setup, the config database is used internally to save information about shards.
Document
The document is a key-value pair (i.e. BSON). MongoDB documents do not need to have the same fields, and the same fields do not need to be of the same data type. This is very different from relational databases and is also a very prominent feature of MongoDB.
A simple document example is as follows:
{"site":"www.php.cn", "name":"php中文网"}
The following table lists the terms corresponding to RDBMS and MongoDB:
RDBMS | MongoDB |
---|---|
Database | Database |
Collection | |
Document | |
Fields | |
Embedded document | |
Primary key (MongoDB provides the key as _id) | |
mongod | |
mongo |
Collection namespaces | Description |
---|---|
dbname.system.namespaces | List all namespaces. |
dbname.system.indexes | List all indexes. |
dbname.system.profile | Contains database profile information. |
dbname.system.users | List all users who can access the database. |
dbname.local.sources | Contains the server information and status of the replication peer (slave). |
There are the following restrictions on modifying objects in the system collection.
Insert data in {{system.indexes}} to create an index. But otherwise the table information is immutable (the special drop index command will automatically update the relevant information).
{{system.users}} is modifiable. {{system.profile}} is removable.
MongoDB data types
The following table shows several commonly used data types in MongoDB.
Data Type | Description |
---|---|
String | String. Commonly used data types for storing data. In MongoDB, UTF-8 encoded strings are legal. |
Integer | Integer value. Used to store numerical values. Depending on the server you use, it can be either 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 key. |
Timestamp | Timestamp. Record the specific time when the document was modified or added. |
Object | is used for embedded documents. |
Null | is used to create null values. |
Symbol | Symbol. This data type is basically identical to the string type, except that it is typically used in languages that use special symbolic types. |
Date | Date time. Stores the current date or time in UNIX time format. You can specify your own date and time: create a Date object and pass in the year, month and day information. |
Object ID | Object ID. The ID used to create the document. |
Binary Data | Binary data. Used to store binary data. |
Code | Code type. Used to store JavaScript code in documents. |
Regular expression | Regular expression type. Used to store regular expressions. |