gpt4 book ai didi

java - MongoDB,使用java同时更新文档的三个字段,有时只更新两个字段

转载 作者:IT老高 更新时间:2023-10-28 13:26:14 25 4
gpt4 key购买 nike

//get the collection
DBCollection coll = MongoDBClient.getInstance().getAlarmInfoCollection();

DBObject query = new BasicDBObject();
query.put(aa, "51d2b09f81b8a943f9e825aa");

DBObject update = new BasicDBObject();
DBObject history = new BasicDBObject();
history.put("ishistory", 1);
history.put("acknowledged", 1);
history.put("state", 1);
update.put("$set", history);
coll.updateMulti(query, update);

之前用程序更新了mongodb中的文档,但有时能成功,有时只更新两个字段(字段,“state”,没有更新),并且propram没有报任何错误。我的程序有什么错误吗?

最佳答案

我编写了一个单元测试来显示代码的行为方式。本单元测试证明:

  1. 您应该能够一次更新多个字段(即可以使用 $set 关键字更新多个字段)
  2. updateMulti 将更新所有匹配的文档

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

@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);

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

关于java - MongoDB,使用java同时更新文档的三个字段,有时只更新两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com