MongoDB Data Model (iii)

Source: Internet
Author: User
Tags mongodb reporting

VI. Data Model Reference documentation

We already know that MongoDB stores data as documents, and the document is a JSON-style data structure consisting of a series of "Field name-value" pairs, as shown below

{"Item": "Pencil", "Qty": $, "type": "No.2"}

Most user-accessible data structures are documents that can represent many meanings, including:

    • All database records.
    • A query selector that defines what records are selected for read, update, and delete operations.
    • Update the definition to define which fields need to be modified.
    • The index specifies which fields are indexed.
    • Data output for MongoDB reporting and configuration, such as Serverstatus output and replication set configuration documentation.
Document Format

MongoDB stores documents on disk in a serialized format of Bson. Bson is the binary representation of JSON documents, although Bson contains more data types than JSON.

MONGO JavaScript scripts are used with MongoDB language drives to convert between Bson and language-related document representations.

Document structure

The form of the document is as follows

{   field1:value1,   field2:value2,   field3:value3,   ...   Fieldn:valuen}

The value of the field can be any of the Bson data types, as well as other documents, arrays, and document arrays. The following sample document contains the various types

var mydoc = {               _id:objectid ("5099803df3f4948bd2f98391"),               "Alan", Last: "Turing" },               new date (' June, 1912 '               ),new date (' June ', 1954 '),               "Turing Machine", "Turing test", "Turingery" ],               views:numberlong (1250000)            }
Database Reference

Because MongoDB's document structure is free and flexible, this makes MongoDB does not support connection operations. Data to normalize storage, or to store relevant data into a document, can eliminate the need for connectivity, which may cause information to be stored in different documents, often in different collections and even in different databases.

MongoDB applications to connect different documents, you can use one of the following two methods:

    • Manual reference to store the _id field of another document in one document. In this way, the application can use a two-time query to return the required data. These references are simple and sufficient in most cases.
    • DBRefs, a document that references another document that includes the _id field, the collection name, and even the database name of the referenced document. By including these names, Dbrefs can easily relate documents in one collection to documents located in other collections. In order to parse the Dbrefs, the program also needs to execute additional queries to get the referenced document. Many drivers have some help to automatically generate queries for Dbref, but the driver does not automatically parse Dbrefs generated documents (some community-supported drivers can toggle behavior, which may automatically parse Dbref as documents). Dbrefs provides a format and type to characterize the relationship between documents. If the database has to interact with multiple frameworks and tools, the Dbrefs format also provides the general semantics for characterizing the links between documents.
Manual reference

Consider the following example to make the _id field of the first document a reference to a second document

original_id = ObjectId () db.places.insert ({    "_id": original_id,    "name": "Broadway Center" ,    " URL ":" Bc.example.net "}) Db.people.insert ({    " name ":" Erin ",    " places_id ": Original_ ID,    "url":  "Bc.example.net/erin"})

This way, once a query is returned from the People collection, you can follow the places_id field values in the returned results to make a second query to the Places collection.

Manual references are very simple, and parsing a reference is also determined by whether the program needs it.

DBRefs

The Dbrefs convention is to characterize a document rather than a specific reference type. Dbrefs contains the name of the collection in addition to the _ID field value, and in some cases the database name.

The format is as follows:

$ref: The field retains the name of the collection where the referenced document is located

$id: Contains the _id field value of the referenced document

$DB: This is an optional option. Contains the name of the database in which the referenced document resides, and note that only some drivers support $db references.

{"$ref": <value>, "$id": <value>, "$db": <value>}

example, consider a document that stores the Dbref in the Creator field

{  "_id": ObjectId ("5126bbf64aed4daf9e2ab771"),  //  ... application fields   "creator" : {                  "$ref": "Creators",                  "$id": ObjectId ("5126bc054aed4daf9e2ab772" ),                  "$db": "Users"               }}

Note that, in general, you can use manual references, and if you need to refer to documents in multiple collections, consider using Dbrefs.

MongoDB Data Model (iii)

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.