2017-07-29 68 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)); 
相关问题