Java MongoDB:更新文档

在本教程中,我们向您展示如何使用Java MongoDB API collection.update()更新文档。

测试数据

假设插入了以下数据/文件。

{
	"hosting" : "hostA",
	"type" : "vps",
	"clients" : 1000
},
{
	"hosting" : "hostB",
	"type" : "dedicated server",
	"clients" : 100
},
{
	"hosting" : "hostC",
	"type" : "vps",
	"clients" : 900
}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

1.使用$ set的DBCollection.update()

查找托管='hostB'的文档,并将其客户端值从100更新为110。

BasicDBObject newDocument = new BasicDBObject();
	newDocument.put("clients", 110);

	BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB");

	collection.update(searchQuery, newDocument);

输出量

{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "clients" : 110}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
文件已替换!!
等等,整个“ hostB”文档被另一个新文档替换,这不是我们想要的。

要仅更新特定值,请使用$set update修饰符。

BasicDBObject newDocument = new BasicDBObject();
	newDocument.append("$set", new BasicDBObject().append("clients", 110));
			
	BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB");

	collection.update(searchQuery, newDocument);

输出量

{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 110}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

注意
MongoDB团队应该创建另一个名为DBCollection.replace()新API,此DBCollection.update() API中有许多新手,它们被误装了。 同样,要更新特定值,请使用$set

2.带有$ inc的DBCollection.update()

本示例说明如何使用$inc修饰符增加特定值。 在hosting ='hostB'处找到文档,通过将值从100增加到199(100 + 99)= 199来更新它的“客户端”值。

BasicDBObject newDocument = 
		new BasicDBObject().append("$inc", 
		new BasicDBObject().append("total clients", 99));
			
	collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);

输出量

{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 199}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

3.具有多个DBCollection.update()

此示例显示了使用multi参数来更新一组匹配的文档。 查找type ='vps'的文档,将所有匹配的文档的'clients'值更新为888。

BasicDBObject updateQuery = new BasicDBObject();
	updateQuery.append("$set", 
		new BasicDBObject().append("clients", "888"));

	BasicDBObject searchQuery = new BasicDBObject();
	searchQuery.append("type", "vps");

	collection.updateMulti(searchQuery, updateQuery);			
	
	//below statement set multi to true.
	//collection.update(searchQuery, updateQuery, false, true);

输出量

{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "clients" : "888" , "type" : "vps"}
注意
如果更新时未将multi设置为true。
BasicDBObject updateQuery = new BasicDBObject();
	updateQuery.append("$set", 
		new BasicDBObject().append("clients", "888"));

	BasicDBObject searchQuery = new BasicDBObject();
	searchQuery.append("type", "vps");

	collection.update(searchQuery, updateQuery);

您会注意到只有第一个匹配的文档被更新。

{"_id":{ "$oid" : "x"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"}
{"_id":{ "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{"_id":{ "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

要更新一组匹配的文档,您需要将“ multi ”设置为true。

4.完整的例子

结合以上代码片段的完整示例。

package com.mkyong.core;

import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * Java MongoDB update document
 * 
 * @author mkyong
 * 
 */

public class UpdateApp {

	public static void printAllDocuments(DBCollection collection) {
		DBCursor cursor = collection.find();
		while (cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}

	public static void removeAllDocuments(DBCollection collection) {
		collection.remove(new BasicDBObject());
	}

	public static void insertDummyDocuments(DBCollection collection) {
		BasicDBObject document = new BasicDBObject();
		document.put("hosting", "hostA");
		document.put("type", "vps");
		document.put("clients", 1000);

		BasicDBObject document2 = new BasicDBObject();
		document2.put("hosting", "hostB");
		document2.put("type", "dedicated server");
		document2.put("clients", 100);

		BasicDBObject document3 = new BasicDBObject();
		document3.put("hosting", "hostC");
		document3.put("type", "vps");
		document3.put("clients", 900);

		collection.insert(document);
		collection.insert(document2);
		collection.insert(document3);
	}

	public static void main(String[] args) {

	try {

	  Mongo mongo = new Mongo("localhost", 27017);
	  DB db = mongo.getDB("yourdb");

	  // get a single collection
	  DBCollection collection = db.getCollection("dummyColl");

	  System.out.println("Testing 1...no $set");

	  insertDummyDocuments(collection);

	  // find hosting = hostB, and update the clients to 110
	  BasicDBObject newDocument = new BasicDBObject();
	  newDocument.put("clients", 110);

	  BasicDBObject searchQuery = new BasicDBObject().append("hosting", "hostB");

	  collection.update(searchQuery, newDocument);

	  printAllDocuments(collection);
	  removeAllDocuments(collection);

	  System.out.println("\nTesting 1...with $set");

	  insertDummyDocuments(collection);

	  BasicDBObject updateDocument = new BasicDBObject();
	  updateDocument.append("$set", new BasicDBObject().append("clients", 110));

	  BasicDBObject searchQuery2 = new BasicDBObject().append("hosting", "hostB");

	  collection.update(searchQuery2, updateDocument);

	  printAllDocuments(collection);
	  removeAllDocuments(collection);

	  System.out.println("\nTesting 2... with $inc");
	  insertDummyDocuments(collection);
	  // find hosting = hostB and increase it's "clients" value by 99
	  BasicDBObject newDocument2 = new BasicDBObject().append("$inc",
		new BasicDBObject().append("clients", 99));

	  collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument2);

	  printAllDocuments(collection);
	  removeAllDocuments(collection);

	  System.out.println("\nTesting 3... with $multi");

	  insertDummyDocuments(collection);
	  // find type = vps , update all matched documents , clients value to 888
	  BasicDBObject updateQuery = new BasicDBObject();
	  updateQuery.append("$set", new BasicDBObject().append("clients", "888"));

	  BasicDBObject searchQuery3 = new BasicDBObject();
	  searchQuery3.append("type", "vps");

	  collection.updateMulti(searchQuery3, updateQuery);
	  // collection.update(searchQuery3, updateQuery, false, true);

	  printAllDocuments(collection);
	  removeAllDocuments(collection);

	  System.out.println("Done");

	} catch (UnknownHostException e) {
		e.printStackTrace();
	} catch (MongoException e) {
		e.printStackTrace();
	}

    }
}

输出量

Testing 1...no $set
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "clients" : 110}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

Testing 1...with $set
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 110}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

Testing 2... with $inc
{ "_id" : { "$oid" : "id"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 199}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

Testing 3... with $multi
{ "_id" : { "$oid" : "id"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{ "_id" : { "$oid" : "id"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
{ "_id" : { "$oid" : "id"} , "clients" : "888" , "hosting" : "hostA" , "type" : "vps"}
Done

参考文献

  1. 如何在MongoDB中进行更新
  2. $ set更新修饰符
  3. $ inc更新修饰符
  4. Java MongoDB API和DBCollection JavaDoc

翻译自: https://mkyong.com/mongodb/java-mongodb-update-document/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值