In MongoDB, the operation to update a single doc is atomic. By default, if an update operation updates more than one doc, the update to each doc is atomic, but for the entire update operation, it is not atomic, and there may be a case where the previous doc update succeeds and the subsequent Doc update fails. Since the operation to update a single doc is atomic, if two updates occur at the same time, one update operation will block the other, and the final result value of Doc is determined by the last update operation.
By using $isolated option, it is possible to ensure that a write operation that updates multiple doc is atomic, and any query operation reads to the update operation until the operation completes (success or failure).
Prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written. By using the $isolated option, you can ensure that no client sees the changes until the operation completes or ER Rors out.
MongoDB will not write to disk in real time when adding and updating data, it may lose data.
One, grammar
db.collection.update ( <query>, <update>, { < Boolean>, <boolean>, <document> })
upsert: Optional. If set to true, creates a new document when no document matches the query criteria. The default value is false, which does isn't insert a new document when no match is found.
Multi:optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false.
Second, update the example
There are three doc in the Users collection, and the insertion code is as follows
user1={name: "T1", age:21}user2={name: "T2", age:22}user3={name: "T3", age:23}db.users.insert ( [User1,user2,user3])
1,upsert option Usage
Upsert option means that if a matching doc exists in the collection, the doc is updated, and if no doc is matched, a new doc is inserted.
Using the update command and the Upsert option, insert a new user,name= "T4"
Db.users.update ({name: "T4"},{name: "T4"},{upsert:True})
For newly inserted user new field:age=24, you must explicitly specify all of the Doc's field.
Db.users.update ({age:24},{name: "T4", age:24})
If you include only age=24 in the update doc, you will lose the Name field, for example
Db.users.update ({name: "T4"},{age:24})
2,multi option Usage
Before using multi option to update multiple doc, consider one question, and if you add 1 to age, leave the other field unchanged at 1 o'clock. This situation requires the use of $inc operator, which increments the value of the specified field without affecting the other fields.
{$inc: {<field1>: <amount1>, <field2>: <amount2>,...}}
Example, the age of the eligible user is added 1
Db.users.update ({age:{$lt: 24}},{$inc: {age:1}},{multi:true})
3, add field for all user
This scenario needs to use $set operator, which replaces the value of the specified field, or adds a new field.
{$set: {<field1>: <value1>, ...}}
The $set operator replaces the value of a field with the specified value. If The field does not exist, $set would add a new field with the specified value, provided the new field does Not violate a type constraint. If you specify a dotted path for a non-existent field, $set 'll create the embedded documents as needed To fulfill the dotted path to the field.
example, add the Sex field for all user, the default value is Femal.
Db.users.update ({},{$set: {sex: "Femal"}},{multi:True})
Third, Atom update multiple doc
In query filter, add $isolated: 1, which means that all doc,update operations that are queried will be done in an atomic operation.
Db.users.update ({$isolated: 1},{$set: {sex: "Femal"}},{multi:True})
Reference doc:
Update Documents
Atomicity and transactions
$isolated
$set
Update operation for MongoDB