Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-53895

updateOne and pull cannot remove content other than the first element

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.3
    • Component/s: Usability
    • None
    • ALL
    • Hide

       

      // mongo mongodb://localhost:27018/test
      // Drop table, Clean up the environment
      db.test_scores.drop()
      
      // Insert test data
      db.test_scores.insertMany(
          [
              {
                  _id: 1, scores: [
                      {_id: 1},
                      {_id: 2},
                      {_id: 3},
                  ]
              },
              {
                  _id: 2, scores: [
                      {_id: 4},
                      {_id: 5},
                      {_id: 6},
                  ]
              }
          ]
          )
      
      // Check data
      // [
      //   {
      //     "_id": 2,
      //     "scores": [
      //       {
      //         "_id": 4
      //       },
      //       {
      //         "_id": 5
      //       },
      //       {
      //         "_id": 6
      //       }
      //     ]
      //   }
      // ]
      db.test_scores.findOne({_id: 2})
      
      // Perform updateOne operation on the second piece of data
      db.test_scores.updateOne(
          {},
          {$pull: {scores: {_id: 5}}}
          )
      
      // Check data, scores._id:5 still in there
      // [
      //   {
      //     "_id": 2,
      //     "scores": [
      //       {
      //         "_id": 4
      //       },
      //       {
      //         "_id": 5
      //       },
      //       {
      //         "_id": 6
      //       }
      //     ]
      //   }
      // ]
      db.test_scores.findOne({_id: 2})
      
      // Check data
      // [
      //   {
      //     "_id": 1,
      //     "scores": [
      //       {
      //         "_id": 1
      //       },
      //       {
      //         "_id": 2
      //       },
      //       {
      //         "_id": 3
      //       }
      //     ]
      //   }
      // ]
      db.test_scores.findOne({_id: 1})
      
      // Perform updateOne operation on the first piece of data
      db.test_scores.updateOne(
          {},
          {$pull: {scores: {_id: 3}}}
          )
      
      // Check data, scores._id:3 is gone
      // [
      //   {
      //     "_id": 1,
      //     "scores": [
      //       {
      //         "_id": 1
      //       },
      //       {
      //         "_id": 2
      //       }
      //     ]
      //   }
      // ]
      db.test_scores.findOne({_id: 1})
      

       

      Show
        // mongo mongodb://localhost:27018/test // Drop table, Clean up the environment db.test_scores.drop() // Insert test data db.test_scores.insertMany( [ { _id: 1, scores: [ {_id: 1}, {_id: 2}, {_id: 3}, ] }, { _id: 2, scores: [ {_id: 4}, {_id: 5}, {_id: 6}, ] } ] ) // Check data // [ // { // "_id" : 2, // "scores" : [ // { // "_id" : 4 // }, // { // "_id" : 5 // }, // { // "_id" : 6 // } // ] // } // ] db.test_scores.findOne({_id: 2}) // Perform updateOne operation on the second piece of data db.test_scores.updateOne( {}, {$pull: {scores: {_id: 5}}} ) // Check data, scores._id:5 still in there // [ // { // "_id" : 2, // "scores" : [ // { // "_id" : 4 // }, // { // "_id" : 5 // }, // { // "_id" : 6 // } // ] // } // ] db.test_scores.findOne({_id: 2}) // Check data // [ // { // "_id" : 1, // "scores" : [ // { // "_id" : 1 // }, // { // "_id" : 2 // }, // { // "_id" : 3 // } // ] // } // ] db.test_scores.findOne({_id: 1}) // Perform updateOne operation on the first piece of data db.test_scores.updateOne( {}, {$pull: {scores: {_id: 3}}} ) // Check data, scores._id:3 is gone // [ // { // "_id" : 1, // "scores" : [ // { // "_id" : 1 // }, // { // "_id" : 2 // } // ] // } // ] db.test_scores.findOne({_id: 1})  

      I am a normal user of mongo. When I use mongoDB to perform a combination of updateOne and pull, its behavior does not meet my expectations. I am not sure if it is a bug or the result of following established rules.

      The official document's description of updateOne is that it will update a single document, but in my test statement, if the selector can hit multiple records, mongo will only check whether the first record meets the requirements, and if it does not meet the requirements, it will not check the following Record whether the requirements are met.

       

      As shown in the steps to reproduce, my question is why do the same operations on id 1 and 2 return different results?

      From what I have observed, the explanation I can give is because id 1 is in the first position, and updateOne will only check this one. Is this correct?

            Assignee:
            edwin.zhou@mongodb.com Edwin Zhou
            Reporter:
            moqimoqidea@gmail.com qingzhi li
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: