Description of documents in MongoDB

Source: Internet
Author: User
Tags documentation mongodb driver

MongoDB uses the Bson format to store data records. Such as:

Document structure

The document consists of a key-value pair with the following structure:

{
Field1:value1,
Field2:value2,
...
Fieldn:valuen
}?

The value of the field can be any Bson data type, including other documents, arrays, and document arrays.

For example, the following documents contain different types of values:

{
_id:objectid ("5099803df3f4948bd2f98391"),
Name: {first: "Alan", Last: "Turing"},
Birth:new Date (' June 23, 1912 '),
Death:new Date (' June 07, 1954 '),
contribs: ["Turing Machine", "Turing test", "Turingery"],
Views:numberlong (1250000)
}?

Analytical:

  • _idis the Objectid type.
  • nameThe value is an embedded document that contains fields first and last .
  • birthand hold death values of the Date type.
  • contribsHolds an array of strings.
  • viewsHolds a value of the numberlong type.
Field Names

Field name is a string type

The document has the following restrictions on field names:

    • The field name is _id reserved for use as the primary key, and its value must be unique within the collection, immutable, and can be any type other than the divisor group.
    • Cannot start with $
    • Cannot contain dot (.) characters
    • Cannot contain null characters

Sometimes Bson documents may have multiple fields that use the same name. In this case, refer to:driver documentation .

Field Value Limit

For indexed collections, the value of the indexed field has a maximum index key length limit. For more information, see Maximum Index Key Length .

Dot symbol

MongoDB uses dot notation to access the elements of an array and access the fields of the embedded document.

Arrays

To specify or access an element of an array through a zero-based index location, concatenate the array name with the point (.) and zero-starting index positions, and enclose them in quotation marks:

"<array>.<index>"?

{
...
contribs: ["Turing Machine", "Turing test", "Turingery"],
...
}?

To access a third character: "contribs.2" .

For examples querying arrays, see:

    • Query an Array
    • Query an Array of Embedded Documents
Embedded documents

To specify or access fields of an embedded document using a point symbol, use the following format: Embedded document name. Field Name:

"<embedded document>.<field>"?

{
...
Name: {first: "Alan", Last: "Turing"},
Contact: {phone: {type: "cell", Number: "111-222-3333"}},
...
}

The top name, contact, and phone embedded inside the contact are embedded documents.

Specify last: In the Name field "name.last" .

In the contact, specify the phone number: "contact.phone.number" .

For examples querying embedded documents, see:

    • Query on embedded/nested Documents
    • Query an Array of Embedded Documents
Limitations of documentation

Documents have the following properties:

Document Size Limit

The maximum value for the Bson document is 16M.

The largest document size helps ensure that a single document cannot use too much RAM, or that excessive bandwidth is used during transmission. To store documents larger than the maximum size, MongoDB provides the Gridfs API. For more information about GRIDFS, see the documentation for the mongofiles and drivers.

Document Field Order

In addition to the following cases, MongoDB retains the order of the document fields after the write operation:

    • _idthe always is the first field of a document
    • renamingField names may cause field reordering.
The _idField

In MongoDB, each document stored in the collection requires a unique _id field as the primary key. If the inserted document omits the _id field, the MongoDB driver automatically generates a Objectid for the _id field.

The _id field has the following behaviors and constraints:

    • By default, MongoDB creates a unique index on the _id field when the collection is created.
    • The _id field is always the first field in a document. If the server receives a document that does not have a _id field, the server moves the field to the beginning.
    • The _id field may contain values for any Bson data type, in addition to arrays.

Common options for _id values:

    • Using ObjectId
    • Use natural unique identifiers, if available. This saves space and avoids the extra indexes.
    • Use self-growing numbers
    • Using UUID
Other uses of the document structure

In addition to defining data records, MONGODB has been using the document structure, including but not limited to:query filters, Update specifications documents, and index Specification documents.

Querying documents

The query filter specifies the condition that the record is selected.

You can use the <field>:<value> expression to specify an equality condition and a query operator expression.

{
<field1>: <value1>,
<field2>: {<operator>: <value>},
...
}?

For examples, see:

    • Query Documents
    • Query on embedded/nested Documents
    • Query an Array
    • Query an Array of Embedded Documents

Give an example of query documents:

Db.inventory.find ({status: "D"})?

Find the record for status = "D" in the collection. Consistent with the statements in sql: inventory

SELECT * from inventory WHERE status = "D"?

Query filter documents You can use query operators to specify conditions in the following form:

{<field1>: {<operator1>: <value1>}, ...}?

The example below shows retrieving inventory records from the collection that have status equal to "a" or "D".

Db.inventory.find ({status: {$in: ["A", "D"]}})?

This statement is consistent with the SQL statement below:

SELECT * from inventory WHERE status in ("A", "D")?

There is also the use of and and or, to look at this document query documents.

Update the specified document

The update document uses update operators to specify data modifications that are performed on the specified field during the Db.collection.update () operation.

{
<operator1>: {<field1>: <value1>,. ...},
<operator2>: {<field2>: <value2>,. ...},
...
}?

For examples, see Update specifications.

Index Specification Document

The index specification document defines the field index and index type:

{<field1>: <type1>, <field2>: <type2>,...}?

Translated from official website: https://docs.mongodb.com/manual/core/document/

Forwarding Annotated Source: http://www.cnblogs.com/jycboy/p/8718320.html

Description of documents in MongoDB

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.