java更新数据多字段_MongoDB,使用java同时更新三个文件字段,有时只更新两个字段...

我编写了一个单元测试来展示代码的行为方式.该单元测试证明:

>您应该能够一次更新多个字段(即

可以使用$set关键字更新多个字段)

> updateMulti将更新所有匹配的文档

(注意,像所有使用TestNG而不是JUnit的Java MongoDB测试一样,但在这种情况下它非常相似)

@Test

public void shouldUpdateAllMatchingFieldsUsingMultiUpdate() throws UnknownHostException {

MongoClient mongoClient = new MongoClient();

DB db = mongoClient.getDB("myDatabase");

DBCollection coll = db.getCollection("coll");

coll.drop();

//Put some test data in the database

for (int i = 0; i < 5; i++) {

DBObject value = new BasicDBObject();

value.put("fieldToQuery", "a");

value.put("ishistory", 2+i);

value.put("acknowledged", 3+i);

value.put("state", 14+i);

value.put("someOtherArbitraryField", Math.random() * 1000);

System.out.println(value);

coll.insert(value);

}

DBObject query = new BasicDBObject("fieldToQuery", "a");

DBObject history = new BasicDBObject().append("ishistory", 1)

.append("acknowledged", 1)

.append("state", 1);

DBObject update = new BasicDBObject("$set", history);

//This syntax for update means that all three fields will be set to the new given value

Assert.assertEquals(update.toString(), "{ \"$set\" : { \"ishistory\" : 1 , \"acknowledged\" : 1 , \"state\" : 1}}");

//Do the update, updating every document that matches the query

coll.updateMulti(query, update);

//find The new values

DBCursor updatedDocuments = coll.find(query);

for (DBObject updatedDocument : updatedDocuments) {

Assert.assertEquals(updatedDocument.get("ishistory"), 1);

Assert.assertEquals(updatedDocument.get("acknowledged"), 1);

Assert.assertEquals(updatedDocument.get("state"), 1);

System.out.println(updatedDocument);

}

}

这个测试通过.对于示例运行,数据库中的数据是:

{ "fieldToQuery" : "a" , "ishistory" : 2 , "acknowledged" : 3 , "state" : 14 , "someOtherArbitraryField" : 700.7831275035031}

{ "fieldToQuery" : "a" , "ishistory" : 3 , "acknowledged" : 4 , "state" : 15 , "someOtherArbitraryField" : 72.65538582882736}

{ "fieldToQuery" : "a" , "ishistory" : 4 , "acknowledged" : 5 , "state" : 16 , "someOtherArbitraryField" : 980.0065367659304}

{ "fieldToQuery" : "a" , "ishistory" : 5 , "acknowledged" : 6 , "state" : 17 , "someOtherArbitraryField" : 91.58266286854722}

{ "fieldToQuery" : "a" , "ishistory" : 6 , "acknowledged" : 7 , "state" : 18 , "someOtherArbitraryField" : 448.19176202797115}

在测试结束时,在使用$set运算符调用updateMulti之后,数据库中的文档是:

{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 700.7831275035031}

{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 72.65538582882736}

{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 980.0065367659304}

{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 91.58266286854722}

{ "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 448.19176202797115}

因此更新已经起作用,将所有匹配文档的三个字段设置为1,而不接触文档上的任何其他数据.

可能值得注意的是,我设置查询,更新和历史记录的语法更具可读性和更短,尽管它应该与原始问题中的代码做同样的事情:

DBObject query = new BasicDBObject("fieldToQuery", "a");

DBObject history = new BasicDBObject().append("ishistory", 1)

.append("acknowledged", 1)

.append("state", 1);

DBObject update = new BasicDBObject("$set", history);

假设您希望所有与您的查询匹配的记录都使用给定值更新,我是否正确?因此你使用updateMulti?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值