Java MongoDB 更新

本文介绍如何使用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更新文档

查询hosting = 'hostB'的文档并把clients的值从100更新为10

使用$set只更新特定的字段。

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

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

collection.updateOne(searchQuery2, updateDocument);

输出:

{ "_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}

2. 使用$inc更新文档

该例子介绍使用$inc修改符增加特定值。

查询hosting = 'hostB'的文档,并把’clients’的值从100增加到199。

Document newDocument2 = new Document().append("$inc",
        new Document().append("clients", 99));

collection.updateOne(new Document().append("hosting", "hostB"), newDocument2);

输出:

{ "_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. 使用updateMany更新多个文档

该例子介绍使用updateMany更新符合条件的多个文档。

查询type = 'vps'的文档,并把所有符合条件的文档的clients更新为888。

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

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

collection.updateMany(searchQuery3, updateQuery);

输出:

{ "_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"}

4. 完整实例

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class UpdateDocument {
    public static void printAllDocuments(MongoCollection<Document> collection) {
        FindIterable<Document> documents = collection.find();
        MongoCursor<Document> mongoCursor = documents.iterator();
        while (mongoCursor.hasNext()) {
            System.out.println(mongoCursor.next());
        }
    }

    public static void removeAllDocuments(MongoCollection<Document> collection) {
        collection.deleteMany(new Document());
    }

    public static void insertDummyDocuments(MongoCollection<Document> collection) {
        Document document = new Document();
        document.put("hosting", "hostA");
        document.put("type", "vps");
        document.put("clients", 1000);

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

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

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

    public static void main(String[] args) {

        try {

            MongoClient mongoClient = new MongoClient("localhost", 27017);
            MongoDatabase database = mongoClient.getDatabase("test");

            // get a single collection
            MongoCollection<Document> collection = database.getCollection("dummyColl");

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

            insertDummyDocuments(collection);

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

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

            collection.updateOne(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
            Document newDocument2 = new Document().append("$inc",
                    new Document().append("clients", 99));

            collection.updateOne(new Document().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
            Document updateQuery = new Document();
            updateQuery.append("$set", new Document().append("clients", "888"));

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

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

            printAllDocuments(collection);
            removeAllDocuments(collection);

            System.out.println("Done");

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

    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值