Java Operations mongodb--Update data

Source: Internet
Author: User
Tags mul tojson

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.