Wolandello
Wolandello

Reputation: 127

Update array of objects using Morphia

Helo! I've got this JSON structure:

 {
    "author" : "some author"
    "comment" : [{
          "text" : "some text",
          "date" : "some date",
       }, {
           "text" : "some text",
          "date" : "some date",
       }]
}

And I need for example to check all comments date. I wrote this code:

    UpdateOperations<Blog> ops;
    Query<Blog> updateQuery = ds.createQuery(Blog.class).disableValidation().filter("comment.$.date", "some date").enableValidation();

    ops = ds.createUpdateOperations(Blog.class).disableValidation().set("comment.$.text", "new text").enableValidation();
ds.update(updateQuery, ops);

But it doesn't work. Could you tell me how to deal with array of objects in morphia?

Upvotes: 1

Views: 1800

Answers (1)

Ross
Ross

Reputation: 18111

The filter shouldn't have the $ operator you can just use dot notation. try:

UpdateOperations<Blog> ops;
Query<Blog> updateQuery = ds.createQuery(Blog.class).field("comment.date").equal("some date");
ops = ds.createUpdateOperations(Blog.class).disableValidation().set("comment.$.text", "new text").enableValidation();

Upvotes: 1

Related Questions