MongoDB (5)-Document document related


  • MongoDB documents can be understood as a row of records in a relational database (Mysql)
  • MongoDB records data as documents in BSON format
  • BSON is a binary representation of JSON documents, but it supports more data types (the next article will talk about it)

Structure of Documents

Teamed by key-value pairs (field name: value)

{   field1: value1,   field2: value2,   field3: value3,   ...   fieldN: valueN}

The value of the field can be any BSON data type, such as: other documents, arrays, document arrays

Small chestnuts

var mydoc = {               _id: ObjectId("5099803df3f4948bd2f98391"),               name: { first: "Alan", last: "Turing" },               birth: new Date('Jun 23, 1912'),               death: new Date('Jun 07, 1954'),               contribs: [ "Turing machine", "Turing test", "Turingery" ],               views : NumberLong(1250000)            }

The above document contains the following data types

  • _id:ObjectId (introduced in the next article)
  • name:文档类型的值,它又包含了 first、last 两个字段值
  • birth、death: Date type value
  • contribs:字符串数组
  • views:NumberLong 类型的值

Field name

The first must be a string, in addition to the following restrictions

Field name cannot contain null characters

Field name is _id

  • Reserved as primary key
  • Its value must be unique in the set and immutable
  • And can be any type other than an array

The top-level field name cannot contain the $ character

However, starting from MongoDB 3.6, it is allowed to store  fields containing  .  And  $ signs

Some restrictions on the field

  • Key names are case sensitive;
  • The value of the key distinguishes the type (such as string and integer, etc.)

Chestnut one

The following two sets of documents are different because of the different types of values


Chestnut II

The following two sets of documents are also different, because the key names are case-sensitive

{ "Recommend" : " 5 "}{"recommend":"5"}

About the field of the same name in a document

  • BSON documents may have multiple fields with the same name
  • However, most MongoDB interfaces use structures that do not support repeated field names (such as hash tables) to represent MongoDB
  • If you need to operate a document with multiple fields with the same name, you need to check the driver-related documents (introduced later)
  • Some documents created by internal MongoDB processes may have duplicate fields, but no MongoDB process will add duplicate fields to existing user documents

Access documentation

It's the same as accessing python's dictionaries  .

Access the array in the document


Array of small chestnuts

Suppose there is a document and want to take the third value of the contribs field

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

Correct way


More methods for querying array fields will be explained in detail later

Access nested documents in documents

"<embedded document>.<field>"

Nested document small chestnut

{   ...   name: { first: "Alan", last: "Turing" },   contact: { phone: { type: "cell", number: "111-222-3333" } },   ...}

Correct way

More methods of nested query will be explained later

Restrictions on field values

There is a limit on the maximum length of the index field (update the article later and update here)

Document limitations

Document size limit

  • Maximum BSON document size is 16 mb
  • The maximum document size helps to ensure that a single document cannot use too much memory or occupy too much bandwidth during transmission
  • In order to exceed the maximum size limit for documents, MongoDB also provides GridFS (more on this later)

Document field order

By default, MongoDB preserves the order of the document fields after the write operation, except in the following cases

  • The _id field is always the first field
  • The update of the renamed field name may cause the reordering of the fields in the document

_id field

  • In MongoDB, each document stored in the collection needs a unique _id field as the primary key
  • If the newly inserted document does not specify the _id field, then MongoDB will automatically generate an ObjectID for it (you can actually see it in the screenshot above)
  • The second article also applies to the update operation through upsert: true (more on this later)

Common options for storing _id values

  • Use ObjectId
  • Use a natural unique identifier (if available), which saves space and avoids additional indexes
  • Generate an auto-incrementing number
  • Generate UUID in the application code, in order to store the UUID value in the collection and index more efficiently, store the UUID as a value of BSON BinData type
  • If the following conditions are met, the BinData type index key will be stored in the index more efficiently:
  • The binary subtype value is between 0-7 or 128-135, and
  • The length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.
  • Use the driver's BSON UUID tool to generate UUID.