Latest web development tutorials

MongoDB analytical concept

Whether we learn what database should learn one of the fundamental concepts in mongodb basic concept is that documents, collections, databases, here we introduce one by one.

The following table will help you easier to understand some of the concepts Mongo:

SQL Terminology / Concepts MongoDB term / concept explain
database database database
table collection Database table / Collections
row document Line data recording / Documentation
column field Data Field / Domain
index index index
table joins Table joins, MongoDB does not support
primary key primary key Primary key, MongoDB automatically _id field as the primary key

By following figure examples, we can more intuitive understanding of Mongo some concepts:


A mongodb can create multiple databases.

The default database MongoDB is "db", the database is stored in the data directory.

A single instance of MongoDB can accommodate multiple independent databases, each with its own set of permissions and different databases are also placed in different files.

"show dbs" command to display a list of all the data.

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
> show dbs
local  0.078GB
test   0.078GB

The implementation of "db" command to display the current database object or collection.

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
> db

Run "use" command, you can connect to a specific database.

> use local
switched to db local
> db

Examples of the above command, "local" is that you want to link database.

In the next chapter we will explain in detail the use of MongoDB in command.

Database also be identified by name. Database name can be any of the following conditions UTF-8 string.

  • It can not be an empty string ( "").
  • Not contain '' (space)., $, /, \ And \ 0 (null Yu Fu).
  • It should be all lowercase.
  • Up to 64 bytes.

Some database names are reserved and can directly access these databases have a special role.

  • admin: from the point of view of the authority, which is the "root" database. If you add a user to the database, the user automatically inherits all the permissions database. Some specific server-side commands can only be run from the database, such as a list of all the database or server.
  • local: This data will never be copied, can be used to store any collection is limited to a single local server
  • config: When Mongo for fragmentation is set to, config database used internally for storing slice information.


Documentation is a key (key-value) of (ie BSON). MongoDB document need not be provided the same field, the same field and does not require the same data type, which is a relational database is very different, MongoDB also very prominent feature.

A simple document examples are as follows:

{"site":"", "name":"本教程"}

The following table lists the RDBMS and MongoDB corresponding terms:

database database
form set
Row File
Row Field
Joint Table Embedded document
Primary key Primary key (MongoDB provides a key for the _id)
Database services and clients
Mysqld / Oracle mongod
mysql / sqlplus mongo

have to be aware of is:

  1. Document key / value pairs are ordered.
  2. Document may be not only the value of the string inside the double quotes can also be several other data types (or even the entire embedded documents).
  3. MongoDB to distinguish the types and sensitive.
  4. MongoDB document can not have duplicate keys.
  5. Key document is a string. With few exceptions, the key can use any UTF-8 characters.

Key documents naming convention:

  • Key can not contain \ 0 (null character). This character is used to indicate the end of the bond.
  • . And $ have special meaning only be used under certain circumstances.
  • To underscore "_" at the beginning of the keys are reserved (not strictly required).


MongoDB is a collection document group, similar to the RDBMS (relational database management system: Relational Database Management System) in the form.

Collection exists in the database, a collection of no fixed structure, which means that you can insert in the collection of data in different formats and types, but usually we insert a set of data will have some relevance.

For example, we can document these different data structures into the collection:


When the first document into the collection will be created.

Legitimate collection name

  • Collection name can not be empty string "."
  • Set name can not contain \ 0 character (null character), this character indicates the end of a collection of names.
  • Set name can not be "system." At the beginning, which is reserved for the system set prefix.
  • User-created collection name can not contain reserved characters. Some drivers do support the collection which contains the name, this is because some system-generated collection contains the character. Unless you want to access a collection of such a system is created, otherwise do not appear in $ name inside.

The following examples:


capped collections

Capped collections are fixed-size collection.

It has high performance characteristics and the queue expired (expired in accordance with the order of insertion). Somewhat similar and "RRD" concept.

Capped collections are automatically inserted in order to maintain high performance object. It is ideal for functions and similar standard logging collection of different, you have to explicitly create a capped collection, a collection of the specified size, in bytes. collection of data storage space values ​​assigned in advance.

It is noted that the specified storage size of the database that contains the header information.

db.createCollection("mycoll", {capped:true, size:100000})
  • In capped collection, you can add new objects.
  • Can be updated, however, the object will not increase the storage space. If you increase the update will fail.
  • The database does not allow deletion. Use drop () method to delete all the rows collection.
  • Note: After you delete, you must explicitly recreate the collection.
  • In 32bit machines, capped collection is the largest storage 1e9 (1X10 9) bytes.


Information in the database is stored in the collection. They use the namespace system:


In MongoDB database namespace <dbname> .system * that contains a variety of system information, special collections (Collection), as follows:

Collection namespace description
dbname.system.namespaces List all namespaces.
dbname.system.indexes List all indexes.
dbname.system.profile It contains the database summary (profile) information.
dbname.system.users List all users can access the database.
dbname.local.sources It contains a copy of the end (slave) server information and status.

To modify the system object in the collection has the following restrictions.

In the {{system.indexes}} insert data, you can create an index. But otherwise the table information is immutable (the special drop index command will automatically update the relevant information).

{{System.users}} are modifiable. {{System.profile}} is deleted.

MongoDB data type

The following table MongoDB several commonly used data types.

type of data description
String String. Storing data commonly used data types. In MongoDB, UTF-8 encoded string is legal.
Integer Integer values. It used to store values. Depending on the server you are using, it can be divided into 32-bit or 64-bit.
Boolean Boolean value. For storing Boolean values ​​(true / false).
Double Double-precision floating-point value. For storing floating-point values.
Min / Max keys The minimum value and a value BSON (binary JSON) element and the highest value of the relative ratio.
Arrays For array or list or store multiple values ​​for a key.
Timestamp Timestamp. Modify or add records document the specific time.
Object For embedded documents.
Null It creates an empty value.
Symbol symbol. This data type is substantially equal to the string type, but the difference is that it is generally for the use of special symbols typed language.
Date Date Time. UNIX time format used to store the current date or time. You can specify your own date and time: Date object is created, the incoming date information.
Object ID Object ID. ID used to create the document.
Binary Data Binary data. For storing binary data.
Code Tag type. JavaScript code is used to store documents.
Regular expression Regular expression type. For storing the regular expression.