MongoDB Concept Analysis
No matter what database we learn, we should learn the basic concepts, the basic concept in MongoDB is the document, the collection, the database, we introduce each one.
The following table will help you to understand some of the concepts in MONGO more easily:
SQL Terminology/Concepts |
MongoDB Terminology/Concepts |
Interpretation/Description |
Database |
Database |
Database |
Table |
Collection |
Database Tables/Collections |
Row |
Document |
Data record rows/documents |
Column |
Field |
Data fields/Fields |
Index |
Index |
Index |
Table joins |
|
Table connection, MongoDB not supported |
Primary key |
Primary key |
Primary key, MongoDB automatically sets the _id field as the primary key |
With the example below, we can also get a more intuitive view of some of the concepts in MONGO:
Database
Multiple databases can be established in a MongoDB.
The default database for MongoDB is "DB", which is stored in the data directory.
A single instance of MongoDB can hold multiple independent databases, each with its own set and permissions, and different databases placed in different files.
the show dbs command displays a list of all the 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
You can connect to a specified database by running the use command.
> Use local
switched to DB local
> DB
Local
In the above instance command, "local" is the database you want to link to.
In the next section we will explain in detail the use of the commands in MongoDB.
The database is also identified by name. The database name can be any UTF-8 string that satisfies the following criteria. Cannot be an empty string (""). Must not contain ' (space) 、.、 $,/, \ and (blank). should be all lowercase. Up to 64 bytes.
Some database names are reserved and direct access to these specially-functioning databases is available. admin: From the permissions point of view, this is the "root" database. If you add a user to this database, the user automatically inherits the permissions of all the databases. Some specific server-side commands can be run only from this database, such as listing all the databases or shutting down the server. Local : This data will never be replicated and can be used to store any Set configthat is limited to a single server locally: When MONGO is used for fragmentation settings, the Config database is used internally to hold information about fragmentation. 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 fields do not require the same data type, which is very different from the relational database, but also MongoDB very prominent features.
A simple documentation example is as follows:
{"Site": "www.runoob.com", "name": "Rookie Tutorial"}
The following table lists the terms corresponding to the RDBMS and MongoDB:
RDBMS |
MongoDB |
Database |
Database |
Form |
Collection |
Yes |
Document |
Column |
Field |
Table Union |
Embed document |
Primary key |
Primary KEY (MongoDB provides key to _id) |
Database Services and clients |
Mysqld/oracle |
Mongod |
Mysql/sqlplus |
Mongo |
Note that the key/value pairs in the document are in order. The values in the document can be not only strings in double quotes, but also several other types of data (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. Except for a few exceptions, the key can use any UTF-8 character.
Document key naming specification: The key cannot contain a (null character). This character is used to indicate the end of the key.. and $ have special meaning that can only be used in certain circumstances. The keys that begin with the underscore "_" are reserved (not strictly required). Collection
A collection is a MongoDB document group, similar to a table in an RDBMS (relational database management system: Relational DB Management systems).
The collection exists in the database, the collection has no fixed structure, which means that you can insert different formats and types of data on the collection, but usually the data we insert into the collection has some relevance.
For example, we can insert documents from the following different data structures into the collection:
{"Site": "Www.baidu.com"}
{"Site": "www.google.com", "name": "Google"}
{"Site": "www.runoob.com", "name": "Rookie Tutorial", "num": 5}
When the first document is inserted, the collection is created. The valid collection name collection name cannot be an empty string "". The collection name must not contain a "char" character (a null character) that represents the end of the collection name. The collection name cannot be "system." Beginning, which is the prefix reserved for the system collection. The collection name created by the user cannot contain reserved characters. Some drivers do support inclusion in the name of a collection because some system-generated collections contain that character. Unless you want to access this set of system creation, do not appear in the name of $.
The following example:
Db.col.findOne ()
capped Collections
Capped collections is a fixed size collection.
It has high performance and the characteristics of the queue expiration (expired in the order of insertion). Somewhat similar to the concept of "RRD".
Capped collections is a high performance automatic maintenance object Insertion Order. It is very suitable for similar logging functions and standard collection different, you have to explicitly create a capped collection, specify a collection size, in bytes. The collection data storage space value is allocated in advance. Note that the specified storage size contains the header information for the database.
Db.createcollection ("Mycoll", {capped:true, size:100000})
In capped collection, you can add new objects. Can be updated, however, objects do not increase storage space. If added, the update will fail. The database is not allowed to delete. Use the drop () method to delete all collection rows. Note: After deletion, you must explicitly recreate the collection. In 32bit machines, the capped collection maximum storage is 1e9 (1x109) bytes.
Meta Data
The information for the database is stored in the collection. They use the system's namespaces:
dbname.system.*
In the MongoDB database, the namespace <dbname>.system.* is a special set (Collection) that contains a variety of system information, as follows:
Collection Namespaces |
Description |
Dbname.system.namespaces |
Lists all the namespace names. |
Dbname.system.indexes |
Lists all indexes. |
Dbname.system.profile |
Contains database overview (profile) information. |
Dbname.system.users |
Lists all users who have access to the database. |
Dbname.local.sources |
Contains the server information and status of the replication pair-side (slave). |
The following restrictions are available for modifying objects in the System collection.
You can create an index by inserting data in {{system.indexes}}. But otherwise the table information is immutable (the Special Drop INDEX command automatically updates the information).
{{System.users}} is modifiable. {{System.profile}}} is removable. MongoDB Data Type
The following table provides several types of data that are commonly used in MongoDB.
Data Type |
Description |
String |
String. Data types that are commonly used for storing data. In MongoDB, UTF-8 encoded strings are legitimate. |
Integer |
Integer value. Used to store numeric values. Depending on the server you are using, it can be divided into 32 or 64 bits. |
Boolean |
A Boolean value. Used to store Boolean values (True/false). |
Double |
A double-precision floating-point value. Used to store floating-point values. |
Min/max keys |
Compares a value to the lowest and highest value of the Bson (binary JSON) element. |
Arrays |
Used to store an array or list or multiple values as a key. |
Timestamp |
Time stamp. Record when the document was modified or added. |
Object |
For inline documents. |
Null |
Used to create a null value. |
Symbol |
Symbol. The data type is essentially the same as the string type, but it is generally used for languages with special symbolic types. |
Date |
Date time. Use the UNIX time format to store the current date or time. You can specify your own date and time: Create date objects, and pass in the date and year 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 |
The type of the regular expression. Used to store regular expressions. Source: http://www.runoob.com/mongodb/mongodb-databases-documents-collections.html |