Java + MongoDB:更新文档中的多个字段

22
我想一次性更新 MongoDB 单个文档中的多个字段,但只有一个字段被更新了。我有一个名为 user 的集合,其中用户由 customer_user_id 唯一定义。我想要更新某个用户的 birth_yearcountry 字段。
这是我的操作:
// Define the search query:
DBCollection col = md.getDb().getCollection("user");
BasicDBObject searchQuery = new BasicDBObject("customer_user_id", customer_user_id);

// Define the update query:
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$set", new BasicDBObject().append("birth_year", birth_year);
updateQuery.append("$set", new BasicDBObject().append("country", country);

log.info("Update query: " + updateQuery);
col.update(searchQuery, updateQuery);

很不幸,只有 country 字段被更新了,更新记录的查询语句如下:

更新查询语句:{"$set": {"country": "奥地利"}}

4个回答

24

我无法验证,但也许你应该尝试一下:

BasicDBObject updateFields = new BasicDBObject();
updateFields.append("birth_year", birth_year);
updateFields.append("country", country);
BasicDBObject setQuery = new BasicDBObject();
setQuery.append("$set", updateFields);
col.update(searchQuery, setQuery);

我认为这差不多是一样的:

updateQuery.put("$set", new BasicDBObject("country",country).append("birth_year", birth_year));

@wawek,我正在尝试您的方法,但是没有任何文档字段被更新。我通过 _id 查询文档,这些文档存在,并尝试推送特定字段的更新,但是没有任何变化发生。 代码:BasicDBObject searchQry = new BasicDBObject("_id", epID); BasicDBObject updateFields = new BasicDBObject(); updateFields.append("isExpired", true); updateFields.append("fetchStatus", FetchStatus.FETCHED.getID()); BasicDBObject setQuery = new BasicDBObject(); setQuery.append("$set", updateFields); UpdateResult updRes = dbC_Episodes.updateOne(searchQry, setQuery); - Mike

17

或者,com.mongodb.client.model.Updates 中也有方便的方法可以执行此操作:

MongoCollection<Document> collection = mongoClient.getDatabase("db").getCollection("user");

collection.updateMany(
    Filters.eq("customer_user_id", customer_user_id),
    Updates.combine(
        Updates.set("birth_year", birth_year),
        Updates.set("country", country)
    ));

使用这种方法会创建一个包含$set的Bson查询,但使用便捷方法可以使您的代码更清晰易读。


根据文档,updateMany更新所有文档而不是单个MongoDB文档中的多个字段。 - Half_Duplex
嗨@Half_Duplex,你能否通过给出适当的例子来证明你的说法。同时,你能否分享包含此声明的MongoDB文档链接。谢谢。 - Dunggeon
combine 创建的是聚合管道还是普通/传统的更新(即单个 set 语句)? - Robert

8

对于 MongoDB 3.4,您可以使用:

MongoCollection<Document> collection = database.getCollection(nameOfCollection);
Bson filter = new Document("SearchKey", Value);   
Bson newValue = new Document("UpdateKey1", "Value1").append("UpdateKey2", "Value2")....;      
Bson updateOperationDocument = new Document("$set", newValue);
collection.updateMany(filter, updateOperationDocument);

是的,这是新的方式。 - WesternGun
1
新的方法不是使用 new Document("$set", new Document("fieldA", "valueA"))。新的方法是使用 com.mongodb.client.model.Updates,正如 @pakat 所回答的那样。 - barrypicker

1
一种基于@pakat的答案的变化...
MongoCollection<Document> collection = mongoClient.getDatabase("db").getCollection("user");

List<Bson> updatePredicates = new ArrayList<Bson>();
Bson predicateBirthYear = set("birth_year", birth_year);
Bson predicateCountry = set("country", country);

updatePredicates.add(predicateBirthYear);
updatePredicates.add(predicateCountry);

collection.updateMany(Filters.eq("customer_user_id", customer_user_id), Updates.combine(updatePredicates));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接