MongoDB Getting Started tutorial one [Documents and collections]

Source: Internet
Author: User

MongoDB is a document-based database for collection storage, and its basic concepts are different from relational databases. For example, in a relational database, we record the information for an order, which is usually the case for designing the table structure:

Design an order basic information table and an order schedule,1 orders have N Order Details, these order details are linked to the order Main table by the foreign key, so to store an order information, need "1+n" record. The order basic information and all order details for the order are organized in MongoDB in a nested JSON format and saved as 1 documents. In other words, the data stored in the "1+n" record is required in the relational database, and MongoDB is saved as 1 JSON-like documents. Therefore, a record (or 1+n record) in a relational database is basically the equivalent of a document from MongoDB. A table in a relational database stores multiple records, and MongoDB uses a "folder" to hold several similar documents and to call such "folders" a collection. Here is a conceptual comparison of MongoDB and relational databases:

Mongodb relational database
DATABSE (Database) DATABSE (Database)
Collection Collection Table (Tables)

document (documentation)

Record/row (record/row)

Field Field

Column (columns)

Index (indexed)

Index (indexed)

Embedded Documents/reference
(Inline document/reference)

Table joins (tables connection)

The document is the core concept of MongoDB, which is essentially a kind of JSON-bson-formatted data. Bson is a JSON-like binary format data that can be understood to add some new data types based on JSON, including dates, Int32, Int64, and so on. Bson is composed of a set of group key-value pairs, which has the characteristics of lightness, ergodic and high efficiency. The ergodic nature is MongoDB will Bson as the primary reason for data storage .

The following issues need to be noted when working with MongoDB documents:

The atomic limit of the write operation in 1.MongoDB is at the document level, and the preservation, modification and deletion of the document are atomic operations;

Because MongoDB is weak transactional, it can be guaranteed by the atomicity of the document when it is necessary to ensure data integrity storage. Or in the case of the above orders, in order to prevent mongodb in saving the main order information and order details when some of the data is not saved successfully, you can save the order master information and order details as a document, so that either the overall preservation of success, or overall failure, the equivalent of achieving the effect of the transaction.

2. A single document occupies no more than 16MB of storage space;

3.MongoDB tries to keep the order of key-value pairs when the document is inserted, but is not strictly guaranteed.

The naming of the document key (field) requires attention to the following points:

1._ID is the system-reserved keyword, which is the default primary key, which must be unique in the collection and cannot be changed

2. The key cannot contain the/or null character, which is used to denote the end of the key

3. Cannot start with $

4. Cannot be included. (Point number)

5. Keys are case-sensitive and cannot be duplicated for example: {foo:1,foo:1}

The naming of collections requires attention to the following points:

1. The collection name cannot be an empty string ("")

2. The collection name cannot contain the \ s character (null character), which represents the end of the collection name

3. The collection name cannot be "system." At the beginning, the prefix is reserved by the system itself

4. The collection name cannot contain the $ character ( Note: Can be included.

To organize a collection with namespaces:

Similar to a folder where a document is stored (or a folder under the B folder, and then the B folder to hold a document), then the path to obtain the document is: A/document (or a/b/document), replace "/" with "." and "a.b. Document", then "a.b." Is the namespace of the document.

1. Add the database name to the name of the collection, in the middle with a dot number, to get the fully qualified name of the set, which is the namespace, for example: namespace Neighbor.blog.

2. Note that the point number can also appear in the collection name, for example: Neighbor.blog.reviews, you can consider the reviews collection as a subset of the blog collection.

3. Using subcollections allows us to better organize the data and make the structure of the data clearer.

MongoDB Getting Started tutorial one [Documents and collections]

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.