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

Updating just the type for a numeric field does not work

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.0.3, 3.0.6
    • 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.
    • Fully Compatible
    • Linux
    • 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 }

      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.

            Assignee:
            sam.kleinman Sam Kleinman (Inactive)
            Reporter:
            joakim.nordling Joakim Nordling
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: