MongoDB Panache Updating nested objects within Document

Viewed 1045

I have a model which looks like this:

{
  "projectName": "MyFirstProject",
  "projectId": "1234",
  "testCaseList": [
    {
      "testCaseName": "TestCase1",
      "steps": [
        {
          "Action": "Click on this",
          "Result": "pass"
        },
        {
          "Action": "Click on that",
          "Result": "pass"
        }
      ]
    },
    {
      "testCaseName": "TestCase2",
      "steps": [
        {
          "Action": "Click on him",
          "Result": "pass"
        },
        {
          "Action": "Click on her",
          "Result": "pass"
        }
      ]
    }
  ]
}

However, as this is a nested object, I am having difficulties updating it using the method:

default PanacheUpdate update(String update, Object... params)

I am using Repository Pattern and below is my code snippet:

List<TestCase> newTestCaseList = ...;
update("testCaseList", newTestCaseList).where("projectId=?1",projectId);

which actually throws the following error:

org.bson.json.JsonParseException: JSON reader was expecting ':' but found ','.
at org.bson.json.JsonReader.readBsonType(JsonReader.java:149)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:82)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
    at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
    at org.bson.BsonDocument.parse(BsonDocument.java:63)
    at io.quarkus.mongodb.panache.runtime.MongoOperations.executeUpdate(MongoOperations.java:634)
    at io.quarkus.mongodb.panache.runtime.MongoOperations.update(MongoOperations.java:629)

My Current Approach

What currently works for me is to use default void update(Entity entity) instead when updating nested objects. This however presents a few considerations:

  1. Extra code is required to fetch the entire document, parse through, and update the required fields
  2. Since update(Entity entity) works on a document level, it will also update unchanged parts of the document, which isn't ideal.
1 Answers

I guess the encountered error states nothing but a limitation of Panache for mongoDB for the moment through the standard offered PanacheQL.

The issue should be worked-around using native mongoDB Java API that can be accessed through the PanacheMongoEntityBase#mongoCollection:

mongoCollection().updateOne(
        eq("projectId", projectId),
        new Document("$set", new Document("testCaseList", newTestCaseList))
);
Related