2014-09-13 19 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()) 
); 

这完美地工作;)

谢谢! 塞缪尔