17

I have a mongo collection like:

{
    "_id": ObjectId("55cad746aed75601b4822cc9"),
    "entityId": "12",
    "entityType": "a",
    "nameIdentity": [{
        "fName": "abc",
        "lName": "def",
        "dob": "00",
        "address": "xyz"
    },

    ]
}

I am using mongodb java 3.0 driver and trying to match and update. For eg: I am trying to match on entityId if it found then add the new nameIdentity.

Second time when I pass

{
    "fName": "123",
    "lName": "456",
    "dob": "00",
    "address": "789"
}

For my entityId: 12 if it matches then my new collection should like this:

{
    "_id": ObjectId("55cad746aed75601b4822cc9"),
    "entityId": "12",
    "entityType": "a",
    "nameIdentity": [{
    "fName": "abc",
    "lName": "def",
    "dob": "00",
    "address": "xyz"
    }, {
    "fName": "123",
    "lName": "456",
    "dob": "00",
    "address": "789"
    }]
}

I want to add it in the same matched object or collection. But its replacing the previous array and adding new like this:

{
    "_id": ObjectId("55cad746aed75601b4822cc9"),
    "entityId": "12",
    "entityType": "a",
    "nameIdentity": [

    {
        "fName": "123",
        "lName": "456",
        "dob": "00",
        "address": "789"
    }
    ]
}

When entity id is matched I want everything to be added not updated. The code I tried is :

mongoDatabase.getCollection("entity").findOneAndUpdate(
    updateDocument, new Document("$set",entityDocument));

I tried with $push and $set. Its creating a new nameIdentity Array. But I want to add in same matched nameIdentity array. Any suggestions where am I going wrong ?

2 Answers 2

15

You should use $push like following:

db.collection.update({
    "entityId": "12"
}, {
    $push: {
    "nameIdentity": {
        "fName": "123",
        "lName": "456",
        "dob": "00",
        "address": "789"
    }
    }
})

Its equivalent query using mongo java driver is something like (tested) :

db.getCollection("entity").updateOne(new Document("entityId", "12"),
new Document("$push", new Document("nameIdentity", new Document("fName", "123").append("lName", "456")
    .append("dob", "00").append("address", "789"))));

If you want to update many documents then use updateMany instead of updateOne by passing required params.

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

4 Comments

If i am passing multiple arrays for eg name {}, address {},qualification{}, is there any shortcut way to update or i have to add all those manually in update query?
@ShaikMujahidAli What do you mean? you want to push multiple objects in nameIdentity with entityId: 12? if yes, create a list of such values and pass it to $push.
after i match entity id 12 ,i need to check fName,lName,address,dob exists if it does then dont do anything else add like above. can u please help its urgent now
You should use $exists to check wheather field exist or not like I checked name here- db.getCollection("entity").updateOne(new Document("entityId", "12").append("nameIdentity.dob",new Document("$exists",false)), new Document("$push", new Document("nameIdentity", new Document("fName", "1223").append("lName", "2222222") .append("dob", "00").append("address", "789"))));
3

You basically want to $push and add to the named array entry here. But for .findOneAndUpdate() you also need to set the ReturnDocument type in order to receive the result.

Otherwise the "original" document is returned, just as it is for all drivers.

    Document entityDocument = new Document();
    entityDocument.append("fname","123");
    entityDocument.append("lname","456");
    entityDocument.append("dob","00");
    entityDocument.append("address","789")

    Document doc = mongoDatabase.getCollection("entity").findOneAndUpdate(
            new Document("entityId", 12),
            new Document("$push", new Document("nameIdentity", entityDocument)),
            new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
    );

    System.out.println(doc.toJson());

2 Comments

If i am passing multiple arrays for eg name {}, address {},qualification{}, is there any shortcut way to update or i have to add all those manually in update query?
@ShaikMujahidAli Well I actually showed you the "correct" syntax here and you chose not to accept. If you have new questions then post additional questions.

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.