2012-07-25 60 views
0

Helo! 我有此JSON結構:使用Morphia更新對象數組

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

我需要例如檢查所有評論日期。我寫了這個代碼:

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

但它不工作。你能告訴我如何處理morphia中的對象數組嗎?

回答

1

該過濾器不應該有$操作符,您可以使用點符號。嘗試:

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(); 
+0

mongo可以使用數組,幾乎就好像它們是單個對象 - 實際上這是一個非常好的功能。 – arikg 2012-07-25 14:52:31

+0

它不起作用。它仍然返回null,但我知道有這樣的記錄。 – Wolandello 2012-07-25 15:18:10

+0

啊,更新了這個例子.. – Ross 2012-07-25 15:24:09