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

updateOne and pull cannot remove content other than the first element

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 4.4.3
    • Fix Version/s: None
    • Component/s: Usability
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      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 })  

      Description

      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?

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: