MongoDB Concept Analysis

Source: Internet
Author: User
Tags install mongodb

MongoDB Concept Analysis

In mongodb, the basic concepts are documents, collections, and databases. We will introduce them one by one.

The following table helps you better understand some Mongo concepts:

SQL terms/concepts MongoDB terms/concepts Description/Description
Database Database Database
Table Collection Database tables/Sets
Row Document Data Record line/Document
Column Field Data Field/Field
Index Index Index
Table joins   Table connection, not supported by MongoDB
Primary key Primary key Primary Key. MongoDB automatically sets the _ id field as the primary key.

Through instances, we can also intuitively understand some concepts in Mongo:

Database

Multiple databases can be created in one mongodb instance.

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

A single MongoDB instance can accommodate multiple independent databases, each of which has its own set and permissions, and different databases are stored in different files.

The "show dbs" command displays a list of all data.

$ ./mongoMongoDB shell version: 3.0.6connecting to: test> show dbsshow dbslocal  0.078GBtest   0.078GB>

Run the "db" command to display the current database object or set.

$ ./mongoMongoDB shell version: 3.0.6connecting to: test> dbtest>

Run the "use" command to connect to a specified database.

> use localswitched to db local> dblocal>

In the preceding instance command, "local" is the database you want to connect.

In the next section, we will explain in detail how to use commands in MongoDB.

The database is also identified by a name. The database name can be any UTF-8 string that meets the following conditions.

  • Cannot be a null string ("").
  • It cannot contain ''(Space),., $,/, \, and \ 0 (empty letter ).
  • All lowercase letters are required.
  • Up to 64 bytes.

Some database names are retained and can directly access these databases with special functions.

  • Admin: From the Perspective of permissions, this is a "root" database. If a user is added to the database, the user automatically inherits the permissions of all databases. Some specific server commands can only be run from this database, such as listing all databases or disabling servers.
  • Local: The data will never be copied and can be used to store any set of data limited to a local server.
  • Config: when Mongo is used for fragment settings, the config database is used internally to save information about the fragment.
Document

A document is a key-value Pair (BSON ). MongoDB documents do not need to set the same fields, and the same fields do not need the same data type. This is very different from relational databases and is also a very prominent feature of MongoDB.

An example of a simple document is as follows:

{"Site": "www.runoob.com", "name": "cainiao tutorial "}

The following table lists the terms corresponding to RDBMS and MongoDB:

RDBMS MongoDB
Database Database
Table Set
Line Document
Column Field
Table Union Embedded document
Primary Key Primary key (MongoDB provides the key as _ id)
Database Service and client
Mysqld/Oracle Mongod
Mysql/sqlplus Mongo

Note that:

  1. Key/value pairs in the document are ordered.
  2. The values in the document can be strings in double quotation marks, or several other data types (or even the entire embedded document ).
  3. MongoDB is case sensitive.
  4. MongoDB documents cannot have duplicate keys.
  5. The document key is a string. Except for a few exceptions, keys can use any UTF-8 character.

Document key naming rules:

  • The key cannot contain \ 0 (null ). This character is used to indicate the end of a key.
  • . And $ are of special significance and can only be used in a specific environment.
  • Keys starting with an underscore (_) are retained (not strictly required ).
Set

A collection is a MongoDB document group, similar to a table in RDBMS (Relational Database Management System: Relational Database Management System.

The Set exists in the database and has no fixed structure. This means that you can insert data of different formats and types into the set, however, data inserted into a set is usually correlated.

 

For example, we can insert the following documents with different data structures into the collection:

{"Site": "www.baidu.com"} {"site": "www.google.com", "name": "Google"} {"site": "www.runoob.com", "name ": "cainiao tutorial", "num": 5}

When the first document is inserted, the set is created.

Valid Set Name
  • The Set name cannot be a Null String "".
  • The Set Name cannot contain \ 0 characters (null characters). This character indicates the end of the Set Name.
  • The Set Name cannot start with "system.", which is the prefix reserved for the system set.
  • The name of the set created by the user cannot contain reserved characters. Some drivers support the inclusion of the Set Name, because some system-generated sets contain this character. Do not show $ in the name unless you want to access the set created by the system.

Example:

db.col.findOne()
Capped collections

Capped collections is a collection of fixed sizes.

It has high performance and queue expiration features (expiration is in the order of insertion). It is somewhat similar to the concept of "RRD.

Capped collections is a high-performance automatic maintenance object insertion sequence. It is very suitable for different functions similar to logging and standard collection. You must create a capped collection explicitly and specify the size of a collection, in bytes. The data storage space value of the collection is allocated in advance.

 

Note that the specified storage size contains the database header information.

 

db.createCollection("mycoll", {capped:true, size:100000})
  • In capped collection, you can add new objects.
  • Objects can be updated. However, objects do not increase storage space. If it is added, the update will fail.
  • The database cannot be deleted. Use the drop () method to delete all rows of the collection.
  • Note: after deletion, you must explicitly recreate the collection.
  • On a 32bit machine, the maximum capped collection size is 1 E9 (1X109) bytes.
Metadata

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

dbname.system.*

In the MongoDB database, the namespace <dbname>. system. * is a special Collection containing multiple types of system information, as follows:

Set namespace Description
Dbname. system. namespaces Lists All namespaces.
Dbname. system. indexes List all indexes.
Dbname. system. profile Contains the profile information of the database.
Dbname. system. users List all users that can access the database.
Dbname. local. sources Contains the server information and status of the replication peer (slave.

The following restrictions apply to modifying objects in a system set.

Insert data in {system. indexes} to create an index. However, the table information is immutable (the special drop index Command automatically updates related information ).

{System. users} can be modified. {System. profile} can be deleted.

MongoDB Data Type

The following table lists several common data types in MongoDB.

Data Type Description
String String. Common data types used to store data. In MongoDB, UTF-8-encoded strings are valid.
Integer Integer value. Used to store numeric values. The server you use can be divided into 32-bit or 64-bit.
Boolean Boolean value. Stores boolean values (true/false ).
Double Double-precision floating point value. Used to store floating point values.
Min/Max keys Compare a value with the minimum and maximum values of BSON (Binary JSON) elements.
Arrays Stores an array, list, or multiple values as one key.
Timestamp Timestamp. Record the document modification or addition time.
Object Embedded document.
Null Creates a null value.
Symbol Symbol. The data type is basically the same as the string type, but the difference is that it is generally used in languages with special symbol types.
Date Date and time. The current date or time is stored in UNIX time format. You can specify your own Date and time: Create a Date object and input the year, month, and day information.
Object ID Object ID. The ID used to create a document.
Binary Data Binary data. Stores binary data.
Code Code type. It is used to store JavaScript code in the document.
Regular expression Regular Expression type. Stores regular expressions.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.