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

Java+;MongoDB:更新文档中的多个字段,java,mongodb,Java,Mongodb,我试图一次更新单个MongoDB文档中的多个字段,但只更新了一个字段。 我有一个集合用户,其中用户由客户\用户\ id唯一定义。我想更新某个用户的出生年份和国家字段 这就是我正在做的: // Define the search query: DBCollection col = md.getDb().getCollection("user"); BasicDBObject searchQuery = new BasicDBObject("customer_user_id", customer_u

我试图一次更新单个MongoDB文档中的多个字段,但只更新了一个字段。 我有一个集合用户,其中用户由客户\用户\ id唯一定义。我想更新某个用户的出生年份国家字段

这就是我正在做的:

// 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);
不幸的是,只有国家/地区字段被更新,记录的updateQuery如下所示:

更新查询:{“$set”:{“国家”:“奥地利”}


我无法验证这一点,但也许您应该尝试:

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

对于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);
MongoCollection collection=database.getCollection(集合名称);
Bson过滤器=新文档(“搜索键”,值);
Bson newValue=新文档(“UpdateKey1”、“Value1”)。追加(“UpdateKey2”、“Value2”)。。。。;
Bson updateOperationDocument=新文档(“$set”,newValue);
collection.updateMany(过滤器、updateOperationDocument);

或者,在
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)
    ));
MongoCollection collection=mongoClient.getDatabase(“db”).getCollection(“用户”);
collection.updateMany(
Filters.eq(“客户\用户\ id”,客户\用户\ id),
更新。合并(
更新.set(“出生年份”,出生年份),
Updates.set(“国家”,国家)
));

这将创建一个带有
$set
的Bson查询,但使用方便的方法可以使代码更清晰易读。

是@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));
MongoCollection collection=mongoClient.getDatabase(“db”).getCollection(“用户”);
List updatePredicates=new ArrayList();
Bson predicateBirthYear=set(“出生年份”,出生年份);
Bson PREDICTECONTRY=set(“国家”,国家);
updatePredicates.add(predicateBirthYear);
updatePredicates.add(谓词国家);
collection.updateMany(Filters.eq(“customer\u user\u id”,customer\u user\u id),Updates.combine(updatePredicates));

@wawek,我正在尝试您的方法,文档的字段都没有更新。我正在通过
\u id
查询文档,这些文档已经存在并试图推送特定字段的更新,但什么也没有发生。代码:BasicDBObject searchQry=新的BasicDBObject(“\u id”,epID);BasicDBObject updateFields=新的BasicDBObject();append(“isExpired”,true);append(“fetchStatus”,fetchStatus.FETCHED.getID());BasicDBObject setQuery=新建BasicDBObject();追加(“$set”,updateFields);UpdateResult updRes=dbC_scents.updateOne(searchQry,setQuery)是的。。这是新方法。新方法不是使用
新文档($set),新文档(“fieldA”,“valueA”)
。新方法是使用
com.mongodb.client.model.Updates
,正如@pakata所回答的那样根据docs,
updateMany
更新所有文档,而不是单个mongodb文档中的多个字段。