1. 程式人生 > >Mongodb中 Documents文檔說明

Mongodb中 Documents文檔說明

常用選項 core 數字 檢索 都是 name documents 名稱 並且

mongodb使用BSON格式存儲數據記錄. 如下圖:

技術分享圖片

文檔結構

文檔有鍵值對組成, 有以下結構:

{
field1: value1,
field2: value2,
...
fieldN: valueN
}?

字段的值可以是任意BSON 數據類型,包括其他文檔, 數組和文檔數組.

例如,以下文檔包含不同類型的值:

{
_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)
}?

解析:

  • _id 是 ObjectId類型.
  • name 值是一個嵌入的文檔,包含字段 first and last.
  • birth and death hold values of the Date type.
  • contribs holds an array of strings.
  • views holds a value of the NumberLong
    type.

Field Names

字段名是String類型

文檔在字段名上有以下限制:

  • 字段名稱 _id保留用作主鍵; 它的值在集合中必須是唯一的,是不可變的,並且可以是除數組以外的任何類型。
  • 不能以$ 開頭
  • 不能包含 點(.) 字符
  • 不能包含空字符

有時候bson 文檔可能有多個字段使用同一個名字.這種情況,參考:driver documentation .

Field Value 限制

對於索引集合,索引字段的值具有最大索引鍵長度限制。 有關詳細信息, SeeMaximum Index Key Length

點符號

MongoDB使用點符號來訪問數組的元素並訪問嵌入文檔的字段。

Arrays

要通過基於零的索引位置指定或訪問數組的元素,請將數組名稱與點(.)和從零開始的索引位置連接起來,並用引號引起來:

"<array>.<index>"?

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

要訪問第三個字符:"contribs.2".

For examples querying arrays, see:

  • Query an Array
  • Query an Array of Embedded Documents

嵌入的文檔

要使用點符號指定或訪問嵌入式文檔的字段,使用以下格式: 嵌入文檔名稱.字段名:

"<embedded document>.<field>"?

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

上邊的 name, contact,以及嵌入在contact裏邊的phone都是嵌入式文檔.

指定name字段中的last : "name.last".

在contact 中指定phone的號碼: "contact.phone.number".

For examples querying embedded documents, see:

  • Query on Embedded/Nested Documents
  • Query an Array of Embedded Documents

文檔的局限性

Documents有以下屬性:

Document Size Limit

bson文檔的最大值是16M.

最大的文檔大小有助於確保單個文檔不能使用過多的RAM,或者在傳輸過程中使用過多的帶寬。 為了存儲大於最大大小的文檔,MongoDB提供了GridFS API。 有關GridFS的更多信息,請參閱mongofiles和驅動程序的文檔。

Document Field Order

除以下情況外,MongoDB保留寫入操作之後的文檔字段的順序:

  • The _id 永遠是文檔的第一個字段
  • renaming 字段名可能會導致字段重排序.

The _id Field

在MongoDB中,存儲在集合中的每個文檔都需要一個唯一的_id字段作為主鍵。 如果插入的文檔省略_id字段,則MongoDB驅動程序自動為_id字段生成一個ObjectId。

_id字段有以下行為和約束:

  • 默認情況下,MongoDB在創建集合時在_id字段上創建一個唯一的索引。
  • _id字段總是文檔中的第一個字段。 如果服務器收到一個沒有_id字段的文檔,那麽服務器將把字段移到開頭。
  • _id字段可能包含任何BSON數據類型的值,除了數組。

_id值的常用選項:

  • 使用 ObjectId
  • 使用自然唯一標識符(如果可用)。 這節省了空間並避免了額外的索引。
  • 使用自增長的數字
  • 使用UUID

文檔結構的其他用途

除了定義數據記錄之外,MongoDB還一直使用文檔結構,包括但不限於:query filters, update specifications documents, and index specification documents.

查詢文檔

查詢過濾器指定紀錄被選中的條件.

你可以使用<field>:<value> 表達式指定相等條件和查詢運算符表達式。

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

For examples, see:

  • Query Documents
  • Query on Embedded/Nested Documents
  • Query an Array
  • Query an Array of Embedded Documents

舉一個Query Documents的例子:

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

inventory 集合中找出status = "D"的記錄. 與sql 中的語句一致:

SELECT * FROM inventory WHERE status = "D"?

查詢過濾器文檔可以使用查詢運算符來指定以下形式的條件:

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

下邊的例子展示從inventory 集合中檢索 status等於"A"或"D"的記錄.

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

這個語句與下邊sql的語句一致:

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

還有and 和or 的用法,想看的看這個文檔Query Documents.

更新指定的文檔

更新文檔使用update operators 來指定在db.collection.update() 操作期間在指定字段上執行的數據修改。

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

For examples, see Update specifications.

索引規範文檔

索引規範文檔定義字段索引和索引類型:

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

翻譯自官網: https://docs.mongodb.com/manual/core/document/

轉發註明出處: http://www.cnblogs.com/jycboy/p/8718320.html

Mongodb中 Documents文檔說明