mongodb的更新語句


轉自:http://www.mongoing.com/docs/tutorial/update-documents.html

更新

MongoDB提供如下方法更新集合中的文檔:

db.collection.updateOne()

即使可能有多個文檔通過過濾條件匹配到,但是也最多也只更新一個文檔。

3.2 新版功能.

db.collection.updateMany()

更新所有通過過濾條件匹配到的文檔.

3.2 新版功能.

db.collection.replaceOne()

即使可能有多個文檔通過過濾條件匹配到,但是也最多也只替換一個文檔。

3.2 新版功能.

db.collection.update()

即使可能有多個文檔通過過濾條件匹配到,但是也最多也只更新或者替換一個文檔。

默認情況下, db.collection.update() 只更新 一個 文檔。要更新多個文檔,請使用 multi 選項。

這些方法接收(如下)參數:

  • 過濾條件文檔—-決定更些哪些文檔。 這些 文檔過濾查詢 使用和讀操作相同的語法:

    • 查詢過濾文檔 能夠用 <field>:<value> 表達式指定相等條件並以此選出所有包含有指定 <value> 的 <field> 的文檔:

      { <field1>: <value1>, ... } 
    • 查詢過濾文檔 能使用以下 ref:查詢操作符 <query-selectors> 來指定查詢條件:

      { <field1>: { <operator1>: <value1> }, ... } 
  • 更新文檔—-指定要執行的修改或替換文檔—完全替換匹配文檔(除了 _id 字段)

  • 選項文檔

行為表現

原子性

MongoDB中所有的寫操作在單一文檔層級上是原子的。更多關於MongoDB和原子性的信息,請參見 原子性和事務處理

_id 字段

一旦設定,你不能更新 _id 字段的值,你也不能用有不同 _id 字段值的替換文檔來替換已經存在的文檔。

文檔大小

當執行更新操作增加的文檔大小超過了為該文檔分配的空間時。更新操作會在磁盤上重定位該文檔。

字段順序

MongoDB按照文檔寫入的順序整理文檔字段,除了 如下的情況:

  • _id 字段始終是文檔中的第一個字段。

  • 包括字段名稱的 renaming 操作可能會導致文檔中的字段重新排序。

在 2.6 版更改: 從2.6版本開始,MongoDB主動嘗試保持字段在文檔中的順序。 2.6版本之前,MongoDB不會主動保持文檔中的字段的順序。

Upsert 選項

如果 db.collection.update()db.collection.updateOne(), db.collection.updateMany() 或者 db.collection.replaceOne() 包含 upsert true 並且 沒有文檔匹配指定的過濾器,那么此操作會創建一個新文檔並插入它。如果有匹配的文檔,那么此操作修改或替換匹配的單個或多個文檔。

更多關於新文檔創建的細節,請參考該方法的詳細頁。

示例集合

本頁示例使用 mongo shell 中的 db.collection.find() 方法。在 mongo shell 中,如果返回的游標沒有賦給使用 var 關鍵字的變量,那么該游標會自動迭代20次 [1] 來打印出結果中的前20個文檔。

要添加示例中涉及的 users 集合,在 mongo shell中運行如下命令:

注解

如果 users 集合中已經包含了相同 _id 值的文檔,你需要在插入示例文檔前 drop 該集合( db.users.drop() )。

db.users.insertMany( [ { _id: 1, name: "sue", age: 19, type: 1, status: "P", favorites: { artist: "Picasso", food: "pizza" }, finished: [ 17, 3 ], badges: [ "blue", "black" ], points: [ { points: 85, bonus: 20 }, { points: 85, bonus: 10 } ] }, { _id: 2, name: "bob", age: 42, type: 1, status: "A", favorites: { artist: "Miro", food: "meringue" }, finished: [ 11, 25 ], badges: [ "green" ], points: [ { points: 85, bonus: 20 }, { points: 64, bonus: 12 } ] }, { _id: 3, name: "ahn", age: 22, type: 2, status: "A", favorites: { artist: "Cassatt", food: "cake" }, finished: [ 6 ], badges: [ "blue", "Picasso" ], points: [ { points: 81, bonus: 8 }, { points: 55, bonus: 20 } ] }, { _id: 4, name: "xi", age: 34, type: 2, status: "D", favorites: { artist: "Chagall", food: "chocolate" }, finished: [ 5, 11 ], badges: [ "Picasso", "black" ], points: [ { points: 53, bonus: 15 }, { points: 51, bonus: 15 } ] }, { _id: 5, name: "xyz", age: 23, type: 2, status: "D", favorites: { artist: "Noguchi", food: "nougat" }, finished: [ 14, 6 ], badges: [ "orange" ], points: [ { points: 71, bonus: 20 } ] }, { _id: 6, name: "abc", age: 43, type: 1, status: "A", favorites: { food: "pizza", artist: "Picasso" }, finished: [ 18, 12 ], badges: [ "black", "blue" ], points: [ { points: 78, bonus: 8 }, { points: 57, bonus: 7 } ] } ] ) 

更新文檔中指定字段

為了修改文檔中的字段,MongoDB 提供了 update operators,例如用來修改值的 $set

為了用update操作執行指定的修改操作,可以使用如下更新文檔的形式。

{
   <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ... } 

像:update:$set`這樣的更新操作符,如果字段不存在則會創建字段。具體參考手冊 :manual:`update operator</reference/operator/update>

db.collection.updateOne()

3.2 新版功能.

下面的例子對 users 集合使用 db.collection.updateOne() 方法來更新*第一個* 根據 過濾條件``favorites.artist`` 等於 "Picasso" 匹配到的文檔更新操作:

  • 使用 $set 操作符更新 favorites.food 字段的值為 "pie" 並更新 type 字段的值為 3,

  • 使用 $currentDate 操作符更新 lastModified 字段的值到當前日期。如果 lastModified 字段不存在, $currentDate 會創建該字段。詳情請參閱 $currentDate.

db.users.updateOne( { "favorites.artist": "Picasso" }, { $set: { "favorites.food": "pie", type: 3 }, $currentDate: { lastModified: true } } ) 

更多信息和例子請參考 db.collection.updateOne()

db.collection.updateMany()

3.2 新版功能.

下面的例子對 users 集合使用 db.collection.updateMany() 方法來更新所有根據過濾條件favorites.artist 等於 "Picasso" 匹配的文檔。更新操作:

  • 使用 $set 操作符更新 favorites.food 字段的值為 "Pisanello" 並更新 type 字段的值為 3,

  • 使用 $currentDate 操作符更新 lastModified 字段的值到當前日期。如果 lastModified 字段不存在, $currentDate 會創建該字段。詳情請參閱 $currentDate.

db.users.updateMany( { "favorites.artist": "Picasso" }, { $set: { "favorites.artist": "Pisanello", type: 3 }, $currentDate: { lastModified: true } } ) 

更多信息和例子請參考: db.collection.updateMany()

db.collection.update

下面的例子對 users 集合使用 db.collection.update() 方法來更新根據過濾條件favorites.artist 等於 "Pisanello" 匹配的 第一個 文檔。更新操作:

  • 使用 $set 操作符把 favorites.food 字段值更新為 "pizza" 並且把 type 字段的值更新為 0

  • 使用 $currentDate 操作符更新 lastModified 字段的值到當前日期。如果 lastModified 字段不存在, $currentDate 會創建該字段。詳情請參閱 $currentDate.

db.users.update( { "favorites.artist": "Pisanello" }, { $set: { "favorites.food": "pizza", type: 0, }, $currentDate: { lastModified: true } } ) 

使用 db.collection.update() 並包含 multi: true 選項來更新多個文檔:

db.users.update( { "favorites.artist": "Pisanello" }, { $set: { "favorites.food": "pizza", type: 0, }, $currentDate: { lastModified: true } }, { multi: true } ) 

文檔替換

要更新除 _id 字段外文檔的整個內容,傳遞一個全新的文檔給 db.collection.replaceOne() 或者 db.collection.update() 作為第二個參數。當替換文檔時,替換的文檔必須僅僅由 <field> :<value> 組成.

替換文檔可以有不同於原文檔的字段。在替換文檔中,由於 _id 字段是不變的,所以,你可以省略 _id字段;不論如何,如果你包含了 _id 字段,它的值必須和當前的值相同。

db.collection.replaceOne

下面的例子對 users 集合使用 db.collection.replaceOne() 方法將通過過濾條件 name 等於 "sue" 匹配到的 第一個 文檔替換為新文檔:

db.users.replaceOne( { name: "abc" }, { name: "amy", age: 34, type: 2, status: "P", favorites: { "artist": "Dali", food: "donuts" } } ) 

db.collection.update

下面的例子對 users 集合使用 db.collection.update() 方法將通過過濾條件 name 等於 "xyz" 匹配到的 第一個 文檔替換為新文檔:

db.users.update( { name: "xyz" }, { name: "mee", age: 25, type: 1, status: "A", favorites: { "artist": "Matisse", food: "mango" } } ) 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM