[JAVA-3811] id is being replaced by _id, of the inner object, when updating data of MongoDB using Java Driver Created: 11/Aug/20  Updated: 13/Aug/20  Resolved: 13/Aug/20

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Akash Shrestha Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm trying to update an object of MongoDB. I'm using Java Driver (Sync).

After a 'create' operation, the data is persisted as follows:

 

{
    "_id" : ObjectId("5f2b7deb62798d1045a47313"),
    "name" : "John",
    "other_info" : {
        "images" : {
            "images" : [ 
                {
                    "id" : "1",
                    "imgType" : "IDBACKIMAGE"
                }, 
                {
                    "id" : "2",
                    "imgType" : "SIGCARDIMAGE"
                }
            ]
        },
    },
    "status" : "PENDING"
}

Now, I want to modify the 'id' parameter of the images array. So I update the data using getCollection().updateOne(filterCondition, combine(updateData)), but it is persisted as below:

 

{
    "_id" : ObjectId("5f2b7deb62798d1045a47313"),
    "name" : "John",
    "other_info" : {
        "images" : {
            "images" : [ 
                {
                    "_id" : "3",
                    "imgType" : "IDBACKIMAGE"
                }, 
                {
                    "_id" : "4",
                    "imgType" : "SIGCARDIMAGE"
                }
            ]
        },
    },
    "status" : "PENDING"
}

As you can see in the updated data, the 'id' property of the images array is now '_id' after update operation. I had provided the JSON with 'id' field but somehow the Mongo Client considered 'id' as '_id' and persisted '_id'. This happens when replaceOne() is used too. This doesn't happen with the create operation as you can see above. Is this an expected behavior when update operation is done? Why is MongoDB treating 'id' and '_id' as same?



 Comments   
Comment by Jeffrey Yemin [ 13/Aug/20 ]

shrestha.akash01@gmail.com looks like you found your answer so I'm closing this issue.

Comment by Akash Shrestha [ 12/Aug/20 ]

Found the answer here: JAVA-2750

Generated at Thu Feb 08 09:00:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.