In Java, you can update a document in a collection by using the Updateone,updatemany,replaceone method. But _id can't be updated.
Updateone only updates a single piece of data, even if multiple data is filtered through filters.lt ("age", 20), and only one is removed for update
Update operator
name Description
$inc Add a specified value
$mul multiplied by a specified value
$rename renaming
$setOnInsert the update operation has no effect on the existing document, but instead inserts a new document, add the specified field to the newly inserted document
$set Modifying values
$unset Delete a specified field in a document
$min update A field in the document that is less than the specified value
$max update A field in the document that is greater than the specified value
$currentDate set the current time, date, or timestamp
$set
Modifies only the specified field value, and when the field does not exist, adds a new field to the document and assigns a value
Doc.updateone ("Age", "Filters.eq", New Document ("$set", New Document ("Sex", 2222));
finditerable<document> iter1 = Doc.find ();
Iter1.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
$inc
Increments the specified field and, when the field does not exist, adds a field to the document and assigns a value
Doc.updateone (Filters.eq ("name", "Zhang San"), New Document ("$inc", New Document ("Age", 10));
Finditerable<document> iter = Doc.find ();
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
$mul
The usage of $mul is similar to that of $inc, the difference is that $mul is multiplied, $inc is added, and if the field does not exist, add a field and assign a value of 0
$rename
Modify the field name of the document
Doc.updateone (Filters.eq ("name", "Zhang San"), New Document ("$rename", New document ("Phone", "Telphone")));
Finditerable<document> iter = Doc.find ();
Iter.foreach (New block<document> () {
public void Apply (Document _doc) {
System.out.println (_doc.tojson ());
}
});
Additional actions to view official documents:
https://docs.mongodb.com/manual/reference/operator/update/
Java Operations mongodb--Update data