"SequoiaDB Giant Sequoia Database" upsert() Overview 3

error

The error information is recorded in the node diagnosis log (diaglog), and you can refer to the error code .

Example

Suppose there are two records in the collection employee:

{
  "_id": {
    "$oid": "516a76a1c9565daf06030000"
  },
  "age": 10,
  "name": "Tom"
}
{
  "_id": {
    "$oid": "516a76a1c9565daf06050000"
  },
  "a": 10,
  "age": 21
}

Update all records in the collection according to the specified update rule, that is, set the rule parameter, and not set the content of the cond and hint parameters. The following operation is equivalent to using the update method to update all records in the set employee, using $inc to increase the value of the age field of the record by 1, and changing the value of the name field to "Mike". For the record without the name field, the $set  operator The name field and its set value will be inserted into the record, and the update result can be viewed using the find method.

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).

Update records according to the access plan. Assuming that the specified index name testIndex exists in the collection, this operation is equivalent to using the update method, using the index named testIndex to access the records whose age field value is greater than 20 in the collection employee, and change the age fields of these records Add 1 to the name.

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).

The partition set sample.employee, the partition key is {a: 1 }, contains the following records

db.sample.employee.find()
{
"_id": {
 "$oid": "5c6f660ce700db6048677154"
},
"a": 1,
"b": 1
}
Return 1 row(s).

Specify the KeepShardingKey parameter: the partition key field in the update rule is not retained. Only the non-partition key b field is updated, and the value of the partition key a field is not updated.

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).

Specify the KeepShardingKey parameter: Keep the partition key field in the update rule. Because updating the partition key is currently not supported, an error will be reported.

db.sample.employee.upsert( { $set: { a: 9 } }, {}, {}, {}, { KeepShardingKey: true } )
(nofile):0 uncaught exception: -178
Sharding key cannot be updated

Click on Jushan Database Documentation Center for more information

Guess you like

Origin blog.csdn.net/weixin_45890253/article/details/112984025