ACEG
ACEG

Reputation: 2041

Java + MongoDB: Updating multiple fields in a document

I am trying to update at once multiple fields in a single MongoDB document, but only one field is updated. I have a collection user, in which users are uniquely defined by a customer_user_id. I want to update a certain user's birth_year and country fields.

This is what I am doing:

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

Unfortunately, only the country field is updated, and the logged updateQuery looks like this:

Update query: { "$set" : { "country" : "Austria"}}

Upvotes: 22

Views: 28729

Answers (4)

barrypicker
barrypicker

Reputation: 10088

A variation on answer by @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));

Upvotes: 1

pakat
pakat

Reputation: 286

Alternatively, there are convenience methods in com.mongodb.client.model.Updates to do this:

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

Underlying this will create a Bson query with $set as well, but using convenience methods keeps your code more clear and readable.

Upvotes: 17

Elf
Elf

Reputation: 81

For MongoDB 3.4 you can use

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

Upvotes: 8

wawek
wawek

Reputation: 1597

I cannot verify that but maybe you should try:

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

or this is pretty the same I think:

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

Upvotes: 24

Related Questions