使用Java更新mongoDB中的特定值

时间:2019-05-21 06:15:46

标签: java arrays json mongodb filter

需要更新mongo数据中的Array中的数据以进行验证。

我需要将QC1中的值“ ValueStatus”更改为“ VERIFIED”,将“ ValueChanged”更改为“ YES”。

我是什么

qcSection = (Document) att.get("QC1");
if (qcSection.getString("InitialValue").equals(initialValue) && qcSection.getString("NewValue").equals(newValue)) {

    BasicDBObject query = new BasicDBObject();
    query.put("QC1", qcSection);

    BasicDBObject data = new BasicDBObject();
    data.put("QC1.$.Status", "VERIFIED");

    BasicDBObject command = new BasicDBObject();
    command.put("$set", data);
    collection.updateOne(query, command);
    break;
}

我在做与@BatScream相同的事情。

我的Mongo数据为

{
    "_id" : ObjectId("46745764576"),
    "DocumentName" : "ABC.doc",
    "CompanyId" : 1,
    "Attributes" : [ 
        {
            "AttributeName" : "Governing Law",
            "AppliedRule" : 12,
            "ApplicationExtractedValue" : {
                "InitialValue" : "India"
            },
            "QC1" : {
                "InitialValue" : "India",
                "NewValue" : "Europe",
                "ValueStatus" : "UNVERIFIED",
                "ValueChanged" : "No",
                "ValueAddedBy" : "iii@iii.com",
                "QCLevel" : 1,
                "QCDate" : ISODate("546533rT07:04:36.756785Z")
            }
        }
    ]
}

需要更新

{
    "_id" : ObjectId("46745764576"),
    "DocumentName" : "ABC.doc",
    "CompanyId" : 1,
    "Attributes" : [ 
        {
            "AttributeName" : "Governing Law",
            "AppliedRule" : 12,
            "ApplicationExtractedValue" : {
                "InitialValue" : "India"
            },
            "QC1" : {
                "InitialValue" : "India",
                "NewValue" : "Europe",
                "ValueStatus" : "VERIFIED",
                "ValueChanged" : "Yes",
                "ValueAddedBy" : "iii@iii.com",
                "QCLevel" : 1,
                "QCDate" : ISODate("546533rT07:04:36.756785Z")
            }
        }
    ]
}

我已经参考以下链接来做到这一点。 updating a value in an array in mongodb from java我没有任何错误。

0 个答案:

没有答案