「SequoiaDB巨杉資料庫」upsert()概述3
阿新 • • 發佈:2021-02-01
技術標籤:資料庫
錯誤
錯誤資訊記錄在節點診斷日誌(diaglog)中,可參考錯誤碼。
示例
假設集合 employee 中有兩條記錄:
{ "_id": { "$oid": "516a76a1c9565daf06030000" }, "age": 10, "name": "Tom" } { "_id": { "$oid": "516a76a1c9565daf06050000" }, "a": 10, "age": 21 }
按指定的更新規則更新集合中所有記錄,即設定 rule 引數,不設定 cond 和 hint 引數的內容。如下操作等效於使用 update 方法,更新集合 employee 中的所有記錄,使用$inc將記錄的 age 欄位值加1,name 欄位值更改為“Mike”,對不存在 name 欄位的記錄,$set操作符會將 name 欄位和其設定的值插入到記錄中,可使用 find 方法檢視更新結果。
db.sample.employee.upsert( { $inc: { age: 1 }, $set: { name: "Mike" } } ) { "UpdatedNum": 2, "ModifiedNum": 2, "InsertedNum": 0 } > db.sample.employee.find() { "_id": { "$oid": "516a76a1c9565daf06030000" }, "age": 11, "name": "Mike" } { "_id": { "$oid": "516a76a1c9565daf06050000" }, "a": 10, "age": 22, "name":"Mike" } Return 2 row(s).
按訪問計劃更新記錄,假設集合中存在指定的索引名 testIndex,此操作等效於使用 update 方法,使用索引名為 testIndex 的索引訪問集合 employee 中 age 欄位值大於20的記錄,將這些記錄的 age 欄位名加1。
db.sample.employee.upsert( { $inc: { age: 1 } }, { age: { $gt: 20 } }, { "": "testIndex" } ) { "UpdatedNum": 1, "ModifiedNum": 1, "InsertedNum": 0 } > db.sample.employee.find() { "_id": { "$oid": "516a76a1c9565daf06050000" }, "a": 10, "age": 23, "name":"Mike" } Return 1 row(s).
分割槽集合 sample.employee,分割槽鍵為 { a: 1 },含有以下記錄
db.sample.employee.find()
{
"_id": {
"$oid": "5c6f660ce700db6048677154"
},
"a": 1,
"b": 1
}
Return 1 row(s).
指定 KeepShardingKey 引數:不保留更新規則中的分割槽鍵欄位。只更新了非分割槽鍵 b 欄位,分割槽鍵 a 欄位的值沒有被更新。
db.sample.employee.upsert( { $set: { a: 9, b: 9 } }, {}, {}, {}, { KeepShardingKey: false } )
{
"UpdatedNum": 1,
"ModifiedNum": 1,
"InsertedNum": 0
}
>
db.sample.employee.find()
{
"_id": {
"$oid": "5c6f660ce700db6048677154"
},
"a": 1,
"b": 9
}
Return 1 row(s).
指定 KeepShardingKey 引數:保留更新規則中的分割槽鍵欄位。因為目前不支援更新分割槽鍵,所以會報錯。
db.sample.employee.upsert( { $set: { a: 9 } }, {}, {}, {}, { KeepShardingKey: true } )
(nofile):0 uncaught exception: -178
Sharding key cannot be updated
點選巨杉資料庫文件中心瞭解更多資訊