1024programmer Mongodb How to update the MongoDB database in the program in Java

How to update the MongoDB database in the program in Java

If the document has been stored, and now you want to add a description attribute to the document, you can update it like this:

Mongo mongo =
“bh_code_a_Java_keywords”>new Mongo(
“bh_code_a_Java_keywords”>this.host,
“bh_code_a_Java_keywords”>this.port);
GridFS fs =
“bh_code_a_Java_keywords”>new GridFS(mongo.getDB(“database”));
GridFSDBFile file = fs.find(
“bh_code_a_Java_keywords”>new ObjectId(document.getId()));
file.put(
“bh_code_a_Java_string”>”description”, document.getDescription());
file. save();

You can also use the update method of DBCollection to update,

DBObject queryCOndition=
“bh_code_a_Java_keywords”>new BasicDBObject();
queryCondition. put(
“bh_code_a_Java_string”>”_id”, document.getId());

Mongo mongo =
“bh_code_a_Java_keywords”>new Mongo(
“bh_code_a_Java_keywords”>this.host,
“bh_code_a_Java_keywords”>this.port);
DB db = mongo.getDB(
“bh_code_a_Java_string”>”database”)
DBCollection c = db.getCollection(
“bh_code_a_Java_string”>”fs.files”);

DBObject updatedValue=
“bh_code_a_Java_keywords”>new BasicDBObject();
updatedValue. put(
“bh_code_a_Java_string”>”description”, document.getDescription());
DBObject updateSetValue=
“bh_code_a_Java_keywords”>new BasicDBObject(
“bh_code_a_Java_string”>”$set”,updatedValue);

c.update(updateSetValue, updateSetValue);

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-update-the-mongodb-database-in-the-program-in-java/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索