Update () and Save () methods
Update () method updates the values in an existing document
Method: The original field
> DB. Collection_name.update (Selection_criteria, Updated_data)
Such as:
The document titled "MongoDB Overview" is set to "New Update MongoDB Overview".
First, the query operation:
> Db.mycol.find ({' title ': ' MongoDB overview '},{' _id ': 1, ' title ': 1})
{"_id": +, "title": "MongoDB Overview"}
Update action
> db.mycol.update ({' title ': ' MongoDB overview '},{$set:{' title ': ' New update ' MongoDB Overview '}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Query for updated results
> Db.mycol.find ({' _id ': 100},{' _id ': 1, ' title ': 1})
{"_id": +, "title": "New Update MongoDB Overview"}
MongoDB only updates one document by default, and if you update multiple documents, set multi to True
>db.mycol.update ({' title ': ' MongoDB Overview '}, {$set: {' title ': ' New update MongoDB overview '}},{multi:true})
Save () method
Grammar:
>db. Collection_name.save ({_id:objectid (), new_data})
MongoDB projection (select field)
MongoDB's Find () method, the second optional parameter that this method receives in the MongoDB query document is the list of fields to retrieve. In MongoDB, when you execute the Find () method, it displays all the fields of the document by default. To limit the fields that are displayed, you need to set the value for the field list to 1 or 0. 1 is used to display fields, and 0 for hidden fields.
Grammar:
>db. Collection_name.find ({},{key:1}) must have an empty curly brace {}, with multiple fields enclosed in a curly brace, separated by commas, shown as 1, hidden as 0
> db.mycollection.find ({},{"title": 1, "_id": 0})
MongoDB Update Documentation