0

I want to update a particular element in an array of mongo with filters.

I have already tried having using BasicDBObject, but than not able to use filter as i have collection of type MongoCollection.

MongoCollection collection = db.getCollection(tnId);

Even tried:

FindIterable<Document> document = collection.find(new BasicDBObject("DocumentName", documentName)                       .append("Attributes.name", "Party 1" ).append("Attributes.value", 12)                   .append("Attributes.actualValue.initialValue", USA));

But in this case What I get is the whole record and than I have to iterate through each Attribute again.

mongo = new MongoClient("localhost", 27017);
MongoDatabase db = mongo.getDatabase(companyName);
MongoCollection collection = db.getCollection(tnId);

Actual data which I am passing is

{
    "DocumentName" : "doc1",
    "Attributes" : [ 
        {
            "name" : "Party 1",
            "value" : 12,
            "actualValue" : {
                "initialValue" : "USA"
            }
        }, 
        {
            "name" : "Party 1",
            "value" : 16,
            "actualValue" : {
                "initialValue" : "SYSTEM"
            }
        }
    ]
}

and I want to search attribute where actualValue is "USA" and Value of attribute is 12 and updated data should look like

{
    "DocumentName" : "doc1",
    "Attributes" : [ 
        {
            "name" : "Party 1",
            "value" : 12,
            "actualValue" : {
                "initialValue" : "USA"
            },
            "updatedvalue" : {
                "initialValue" : "USA",
                "changedValue" : "Europe"
             }

        }, 
        {
            "name" : "Party 1",
            "value" : 16,
            "actualValue" : {
                "initialValue" : "SYSTEM"
            }
        }
    ]
}

3 Answers 3

1

db.collection.findOneAndUpdate({ "Attributes.actualValue.initialValue": "USA" }, 
  { $set: { "Attributes.$.updatedvalue.initialValue": "USA",      "Attributes.$.updatedvalue.changedValue": "Europe" } })

db.collection.findOneAndUpdate({ "Attributes.actualValue.initialValue": "USA" }, { $set: { "Attributes.$.updatedvalue.initialValue": "India", "Attributes.$.updatedvalue.changedValue": "India" } })

Sign up to request clarification or add additional context in comments.

Comments

0

Try with this one

db.collection.updateMany(
 { "Attributes.actualValue.initialValue": "USA" },
 { $set: { "Attributes.$.updatedvalue" : {
            "initialValue" : "USA",
            "changedValue" : "Europe"
         } } }
)

1 Comment

I want filter on the basis of name,value and actualValue.initialValue all 3. As my data can be repeated.Once I get data than update only that data. And how would I write it in Java.I want something like Bson updateDocument = new Document("$set", d); collection.updateOne(filter, updateDocument); SO how to write a filter?
0

How I did is. I made a document where i added teh required data which i want to update it with.Say

Document valueDocument = new Document();

valueDocument.put("InitialValue", USA);

BasicDBObject query = new BasicDBObject();
query.put("Attributes", att);

BasicDBObject data = new BasicDBObject();
data.put("Attributes.$.updatedvalue" + qCLevel, valueDocument);

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

It goes and insert the data in the right place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.