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

Casted type-change updates are discarded

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.1
    • Component/s: Shell
    • None
    • ALL
    • Hide

      A simple example:

      // insert a document into a new table; number is interpreted as a double
      db.example.insert({"foo": 1})
      //--> WriteResult({ "nInserted" : 1 })
      
      // good so far
      db.example.find()
      //--> { "_id" : ObjectId("56b4c6dcbfa50a533004158a"), "foo" : 1 }
      
      // confirm the field is of type double
      // ref: https://docs.mongodb.org/manual/reference/operator/query/type
      db.example.count({"foo": {"$type": 1}})
      //--> 1
      
      // attempt to update field from double to integer; fails
      db.example.update({"foo": {"$type": 1}}, {"$set": {"foo": NumberInt(1) }})
      //--> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
      
      // doc still has a double
      db.example.count({"foo": {"$type": 1}})
      //--> 1
      
      // update field from double to integer of a different value; succeeds
      db.example.update({"foo": {"$type": 1}}, {"$set": {"foo": NumberInt(2) }})
      // WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      
      // doc no longer has a double
      db.example.count({"foo": {"$type": 1}})
      //--> 0
      
      // confirm doc now has an int
      db.example.count({"foo": {"$type": 16}})
      //--> 1
      
      Show
      A simple example: // insert a document into a new table; number is interpreted as a double db.example.insert({ "foo" : 1}) //--> WriteResult({ "nInserted" : 1 }) // good so far db.example.find() //--> { "_id" : ObjectId( "56b4c6dcbfa50a533004158a" ), "foo" : 1 } // confirm the field is of type double // ref: https://docs.mongodb.org/manual/reference/ operator /query/type db.example.count({ "foo" : { "$type" : 1}}) //--> 1 // attempt to update field from double to integer; fails db.example.update({ "foo" : { "$type" : 1}}, { "$set" : { "foo" : NumberInt(1) }}) //--> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) // doc still has a double db.example.count({ "foo" : { "$type" : 1}}) //--> 1 // update field from double to integer of a different value; succeeds db.example.update({ "foo" : { "$type" : 1}}, { "$set" : { "foo" : NumberInt(2) }}) // WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) // doc no longer has a double db.example.count({ "foo" : { "$type" : 1}}) //--> 0 // confirm doc now has an int db.example.count({ "foo" : { "$type" : 16}}) //--> 1

      When attempting to update a document by changing the type of a field, that update appears to be discarded if the result is casted-equivalent. So far, I've observed this behavior when attempting to replace a double with an int of the same value.

      Combined with SERVER-22489, this became an irritating problem; the only workaround I have so far is to update with some erroneous value, then update a second time to fix it.

            Assignee:
            Unassigned Unassigned
            Reporter:
            NathanielKofalt Nathaniel Kofalt
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: