Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3811

id is being replaced by _id, of the inner object, when updating data of MongoDB using Java Driver

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • Labels:
      None

      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?

            Assignee:
            Unassigned Unassigned
            Reporter:
            shrestha.akash01@gmail.com Akash Shrestha
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: