2017-07-29 67 views
0

我的收藏是這樣的:

{ 
"_id" : ObjectId("597c4c42398593a7b464fc9c"), 
"userId" : NumberLong(2), 
"steps" : [ 
    { 
     "_id" : ObjectId("597c4c42398593a7b464fc9a"), 
     "beginningDate" : "2017-07-29T13:20:10.344", 
     "state" : "Pending", 
     "messages" : [ 
      { 
       "_id" : ObjectId("597c4c42398593a7b464fc9b"), 
       "content" : "Hi", 
       "isRead" : 0, 
       "side" : "UserToAdmin", 
       "creationDate" : "2017-07-29T13:20:10.344" 
      } 
     ] 
    }, 
    { 
     "_id" : ObjectId("597c4ce5398593aaa897ccb4"), 
     "beginningDate" : "2017-07-29T13:22:53.884", 
     "state" : "Open", 
     "messages" : [] 
    } 
], 
"lastStepState" : "Pending", 
"lastModified" : "2017-07-29T13:26:36.774" 
} 

什麼基本上,我試圖做的是,每當我推一個新的臺階進入步驟陣,我在下面的方式更新lastStepState:

Document updateQueryDoc = new Document("userId", userId).append("lastStepState", 
       new Document("$eq", State.Pending.name())); 
     Document updateDoc = new Document("$push", new Document("steps", newStepDoc)) 
       .append("$set", new Document("lastStepState", State.Open.name())) 
       .append("$set", new Document("lastModified", now)); 

(狀態與等待和開放值的枚舉) 然而,lastStepState不會被更新。可能是什麼問題呢? (我還要提到的是有集合中的一個文件,所以使用updateMany不是soultion我的問題。)

回答

0

文檔的附加使用了底層地圖的put(K key, V value)功能,所以當你調用append("$set", new Document("lastModified", now))它覆蓋的值以前設置的$set鍵。

你能解決這個問題是這樣的:

Document updateDoc = new Document("$push", new Document("steps", newStepDoc)) 
    .append("$set", new Document("lastStepState", State.Open.name()).append("lastModified", now)); 
相關問題