使用java在mongodb中进行多次更新

使用java在mongodb中进行多次更新,java,mongodb,mongodb-query,Java,Mongodb,Mongodb Query,我收到了这份文件: { "_id" : ObjectId("54140782b6d2ca6018585093"), "user_id" : ObjectId("53f4ae1ae750619418a20467"), "date" : ISODate("2014-09-13T08:59:46.709Z"), "type" : 0, "tot" : 2, "additional_info" : {

我收到了这份文件:

{
        "_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
                }
        ]
}
我需要在第一个事务对象中再添加两个字段: -执行日期:日期 -结果:此bson文档

{ "server_used" : "xxx.xxx.xxx.xxx:27017" , "ok" : 1 , "n" : 1 , "updated_executed" : true} (m_OR.getDocument() in the following code example)
为了得到那份文件

    {
        "_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
                }
        ]
}
我能够做到这一点的唯一方法是Do2分离更新(更新是一个my wrapper函数,它在mongodb中执行真正的更新,并且工作正常):

如果我尝试这样做:

    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);
仅应用第二组

有没有办法避免双重执行,只需一次调用即可应用更新?

hash/map对象的基本规则是只能有一个键。这是“高地人”规则(“只能有一个”)在一般情况下适用。因此,只需采用不同的方法:

BasicDBObject value = new BasicDBObject();
value.put("$set",
    new BasicDBObject("transactions.$.date_execution",new Date())
        .add( new BasicDBObject("transactions.$.result",m_OR.getDocument() )
);
因此,基本上“两个”字段参数都是“$set”语句的一部分,如序列化形式:

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

这基本上就是您最终想要的。

您的建议是正确的,只需通过以下方式修改一下语法:

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

谢谢! 塞缪尔

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