2014-09-13 48 views
0

我有這個文件:多個更新用java

{ 
     "_id" : ObjectId("54140782b6d2ca6018585093"), 
     "user_id" : ObjectId("53f4ae1ae750619418a20467"), 
     "date" : ISODate("2014-09-13T08:59:46.709Z"), 
     "type" : 0, 
     "tot" : 2, 
     "additional_info" : { 
       "item_id" : ObjectId("540986159ef9ebafd3dcb5d0"), 
       "shop_id" : ObjectId("53f4cc5a6e09f788a103d0a4"), 
       "ap_id" : ObjectId("53f4cc5a6e09f788a103d0a5") 
     }, 
     "transactions" : [ 
       { 
         "_id" : ObjectId("54140782b6d2ca6018585091"), 
         "date_creation" : ISODate("2014-09-13T08:59:46.711Z"), 
         "type" : -1 
       }, 
       { 
         "_id" : ObjectId("54140782b6d2ca6018585092"), 
         "date_creation" : ISODate("2014-09-13T08:59:46.788Z"), 
         "type" : 1 
       } 
     ] 
} 

,我需要2個字段添加到第一筆交易opbject: - date_execution:日期 - 結果:本BSON文檔

{ "server_used" : "xxx.xxx.xxx.xxx:27017" , "ok" : 1 , "n" : 1 , "updated_executed" : true} (m_OR.getDocument() in the following code example) 

到obtaing該文件

{ 
     "_id" : ObjectId("54140811b6d25137753c1a1a"), 
     "user_id" : ObjectId("53f4ae1ae750619418a20467"), 
     "date" : ISODate("2014-09-13T09:02:09.098Z"), 
     "type" : 0, 
     "tot" : 2, 
     "additional_info" : { 
       "item_id" : ObjectId("540986159ef9ebafd3dcb5d0"), 
       "shop_id" : ObjectId("53f4cc5a6e09f788a103d0a4"), 
       "ap_id" : ObjectId("53f4cc5a6e09f788a103d0a5") 
     }, 
     "transactions" : [ 
       { 
         "_id" : ObjectId("54140811b6d25137753c1a18"), 
         "date_creation" : ISODate("2014-09-13T09:02:09.100Z"), 
         "type" : -1, 
         "result" : { 
           "server_used" : "xxx.xxx.xxx.xxx:27017", 
           "ok" : 1, 
           "n" : 1, 
           "updated_executed" : true 
         }, 
         "date_execution" : ISODate("2014-09-13T09:02:15.370Z") 
       }, 
       { 
         "_id" : ObjectId("54140811b6d25137753c1a19"), 
         "date_creation" : ISODate("2014-09-13T09:02:09.179Z"), 
         "type" : 1 
       } 
     ] 
} 

唯一這樣我能做到這一點是做2分離更新(更新是執行MongoDB中的實際更新一個我的包裝funciont,它工作正常):

// where 
    BasicDBObject query = new BasicDBObject(); 
    query.append("transactions._id", m_Task.ID()); 

    // new value for result - 1st upd 
    BasicDBObject value = new BasicDBObject(); 
    value.put("$set",new BasicDBObject("transactions.$.date_execution",new Date())); 
    update(this._systemDB, "activities", query, value); 

    // new value for date_execution - 2nd upd 
    value = new BasicDBObject(); 
    value.put("$set",new BasicDBObject("transactions.$.result",m_OR.getDocument())); 
    update(this._systemDB, "activities", query, value); 

如果我試着這樣做:

BasicDBObject value = new BasicDBObject(); 
    value.put("$set",new BasicDBObject("transactions.$.date_execution",new Date())); 
    value.put("$set",new BasicDBObject("transactions.$.result",m_OR.getDocument())); 
    or = update(this._systemDB, "activities", query, value); 

只是第二套將被應用。

有沒有辦法避免雙重執行,並只需要一次調用即可應用更新?

回答

1

「哈希/地圖」對象的基本規則是,你只能有一個鍵。這是「高地球員」規則(「可能只有一個」)一般原因適用。因此,只要適用不同:

BasicDBObject value = new BasicDBObject(); 
value.put("$set", 
    new BasicDBObject("transactions.$.date_execution",new Date()) 
     .add(new BasicDBObject("transactions.$.result",m_OR.getDocument()) 
); 

所以基本上「都」字段參數爲「$組」的部分語句的序列化形式:

{ 
    "$set": { 
     "transactions.$.date_execution": new Date(), 
     "transactions.$.result": m_Or.getDocument() 
    } 
} 

基本上是你到底想要什麼。

0

你的建議是正確的,只是要解決一個小的語法是這樣的:

BasicDBObject value = new BasicDBObject(); 
value.put("$set", 
     new BasicDBObject("transactions.$.date_execution",new Date()) 
     .append("transactions.$.result",m_OR.getDocument()) 
); 

這完美地工作;)

謝謝! 塞繆爾