Updates in MONGO can also be used as additions
There are several ways to MONGO with the new
Save, UPDATE, Upsert
When you execute save, if the document has _id this parameter, save calls Upsert, otherwise it calls the insert
Upsert modified faster than update
Use Upsert if the modified data does not exist, a new data is created
Various modifiers
$inc, $set
Where $inc can only modify data of numeric types, modifying other types of data will cause an error
Db.ansheng.update ({"AAAAA": 20},{"$inc": {"AAAAA": -19}},true) Upsert
Db.ansheng.update ({"AAAAA": 20},{"$inc": {"AAAAA": -19}}) update
Db.ansheng.save ({"_id": ObjectId ("5706032acd0a6194868cf53e"), "AAAAA":) Save
When you use Save, be aware that the save changes are equivalent to replacing the document.
For example: The above save statement, when modifying the target data will only modify the AAAAA this field, the other fields will be erased
When using save, if the previous query parameter is not _id, he will create a new document
There are three simplest modifications, all of which are modified only by one field
When you use Save, it affects other fields.
Using update and Upsert will not affect other fields
Modification of arrays
{ "_id": ObjectId ("5706032acd0a6194868cf53e"), "AAAAA":20.0, " Book": ["looking for hope in despair","C # Daquan","MONGO authoritative Guide"]}
Modify the data in book
Db.ansheng.update ({"AAAAA": 20},{"$set": {"book.0": "Dry Your Sister 3,000"}},true)
Db.ansheng.update ({"AAAAA": 20},{"$set": {"book.0": "Dry Your Sister 3,000"})
Db.ansheng.save ({"_id": ObjectId ("5706032acd0a6194868cf53e"), "AAAAA": "Book": ["Looking for hope in despair", "C # Daquan", "MONGO authoritative guide"]} )
Summarize
Save, UPDATE, Upsert
Save can only be modified as a whole
Both update and Upsert can implement partial modifications
Update changes will not be modified if the same content is modified
Upsert will be modified regardless of the difference
Because the update changes the time will be first to query the database, if the same will not be modified, so the efficiency of update will be lower than upsert
So personal feeling
Upsert is the most efficient
The efficiency of the update is second
Save is the least efficient
Summary of updates in MONGO