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

Updating just the type for a numeric field does not work

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 3.0.3, 3.0.6
    • Fix Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Environment:
      Seen on CentOS 7.1, both in 3.0.3 and 3.0.6. Not tested on anything else.
    • Backwards Compatibility:
      Fully Compatible
    • Operating System:
      Linux
    • Steps To Reproduce:
      Hide

      1. Let's store the value 0 as double and verify it indeed is a double:

      > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": 0} }, {upsert: true})
      WriteResult({
      	"nMatched" : 0,
      	"nUpserted" : 1,
      	"nModified" : 0,
      	"_id" : ObjectId("55f6c676e779893076f77e6b")
      })
      > db.test.find( { test: { $type: 1 } } );
      { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 0 }
      > db.test.find( { test: { $type: 16 } } );
      

      2. Try to replace it with value 0 as a 32-bit integer and verify it's still a double:

      > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": NumberInt(0)} }, {upsert: true})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
      > db.test.find( { test: { $type: 1 } } );
      { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 0 }
      > db.test.find( { test: { $type: 16 } } );
      

      3. Change the number to 1 and make sure it's a 32-bit integer after this:

      > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": NumberInt(1)} }, {upsert: true})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.test.find( { test: { $type: 1 } } );
      > db.test.find( { test: { $type: 16 } } );
      { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 1 }
      

      4. Now let's change the value successfully to 0 as a 32-bit integer:

      > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": NumberInt(0)} }, {upsert: true})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.test.find( { test: { $type: 1 } } );
      > db.test.find( { test: { $type: 16 } } );
      { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 0 }
      

      Show
      1. Let's store the value 0 as double and verify it indeed is a double: > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": 0} }, {upsert: true}) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("55f6c676e779893076f77e6b") }) > db.test.find( { test: { $type: 1 } } ); { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 0 } > db.test.find( { test: { $type: 16 } } ); 2. Try to replace it with value 0 as a 32-bit integer and verify it's still a double: > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": NumberInt(0)} }, {upsert: true}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.test.find( { test: { $type: 1 } } ); { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 0 } > db.test.find( { test: { $type: 16 } } ); 3. Change the number to 1 and make sure it's a 32-bit integer after this: > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": NumberInt(1)} }, {upsert: true}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.test.find( { test: { $type: 1 } } ); > db.test.find( { test: { $type: 16 } } ); { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 1 } 4. Now let's change the value successfully to 0 as a 32-bit integer: > db.test.update({"_id" : ObjectId("55f6c676e779893076f77e6b")}, { $set: {"test": NumberInt(0)} }, {upsert: true}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.test.find( { test: { $type: 1 } } ); > db.test.find( { test: { $type: 16 } } ); { "_id" : ObjectId("55f6c676e779893076f77e6b"), "test" : 0 }

      Description

      If you store the value 0 as a double in a field, you can't change the value 0 to a 32 bit integer without switching the value to something else in between.
      It appears MongoDB on update considers 0 (the double) to be equal to the new value 0 (32-bit integer) and does not do any update.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: