0

How could I update a specific field in subdocument of array element ?

My question is similar to the following below, but in my case I need to update just a subdocument value.

MongoDB: How do I update a single subelement in an array, referenced by the index within the array?

I have the following documento model :

{
  _id : "xpto",
  other_stuff ... ,
  templates : [
    { 
        templateId:"template-a" 
        body: {
            en_US:"<p>Hello World!</p>"
        }
    },
    { 
        templateId:"template-b" 
        body: {
            es_ES:"<p>Holla !</p>"
        }
    }
  ]
}

So, In mongodb shell the following statement works perfectly for me:

db.apiClient.update({"_id":"xpto","templates.templateId":"template-b"}, {$set:{"templates.$.body.es_ES":"<h1>Gracias !</h1>"}})

However , when i try to do it with Mongo Java Driver , I get an IllegalArgumentException.

    BasicDBObject selectQuery = new BasicDBObject("_id", "xpto");
    selectQuery.put("templates.templateId", "template-b");

    BasicDBObject updateQuery = new BasicDBObject();
    for(String locale : template.getBody().keySet()) {

        String updateBodyLocaleExpression = new StringBuilder()
                .append("templates.$.body.").append(locale).toString();

        String updateBodyLocaleValue = template.getBody().get(locale);

        updateQuery.put(updateBodyLocaleExpression, updateBodyLocaleValue);

    }

    updateQuery.put("$set", updateQuery);
    getCollection(COLLECTION_NAME).update(selectQuery, updateQuery, true, true);

It throws the following exception :

Caused by: java.lang.IllegalArgumentException: Invalid BSON field name templates.$.body.es_ES
        at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:494)
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:127)
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
        at com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:253)
        at com.mongodb.connection.RequestMessage.addCollectibleDocument(RequestMessage.java:219)
        at com.mongodb.connection.UpdateMessage.encodeMessageBodyWithMetadata(UpdateMessage.java:77)
        at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
        at com.mongodb.connection.WriteProtocol.execute(WriteProtocol.java:85)

Is something wrong with my code ?

Thanks.

Community
  • 1
  • 1
Eduardo Fabricio
  • 2,151
  • 2
  • 25
  • 32

1 Answers1

1

Yes. You construct wrong updateQuery. You put fields templates.$.body... in the BasicDbObject and then add same document into $set field into itself. MongoDB tries to update field templates.$.body. and here $ is the part of the field name instead of operator.

Here is working example:

    //List is for testing purposes only
    List<String> locales = Arrays.asList("en_US", "en_UK");

    Document query = new Document("_id", "xpto")
            .append("templates.templateId", "template-b");

    Document updateQuery = new Document();
    for (String locale : locales) {
        updateQuery.put("templates.$.body." + locale, "<pre>Updated " + locale + "</pre>");
    }
    collection.updateOne(query, new Document("$set", updateQuery));

Document is almost the same as BasicDbObject but more general.

Vadeg
  • 1,156
  • 10
  • 22