Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-563 documentation for valueOf() and toString()
  3. DOCS-584

Release notes for 2.2 should mention breaking change: ObjectId().valueOf() and ObjectId().toString() have reversed meanings

    XMLWordPrintableJSON

Details

    • Icon: Sub-task Sub-task
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • Server_Docs_20231030
    • None
    • Server
    • None

    Description

      We changed the result returned by the valueOf() and toString() methods of ObjectId() in 2.1.0 and later compared to the 2.0 series.

      Version 2.0

      MongoDB shell version: 2.0.8-rc0-pre-
      connecting to: test
      > var o = new ObjectId()
      > o
      ObjectId("507172c510b40cc18add2475")
      > o.str
      507172c510b40cc18add2475
      > o.valueOf()
      ObjectId("507172c510b40cc18add2475")
      > o.toString()
      507172c510b40cc18add2475
      > o.tojson()
      ObjectId("507172c510b40cc18add2475")
      > typeof o
      object
      > typeof o.str
      string
      > typeof o.valueOf()
      object
      > typeof o.toString()
      string
      > typeof o.tojson()
      string
      > o.valueOf
      function valueOf() {
          [native code]
      }
      > o.toString
      function toString() {
          [native code]
      }
      > o.tojson
      function () {
          return "ObjectId(\"" + this.str + "\")";
      }
      >

      Version 2.1, 2.2 and higher

      MongoDB shell version: 2.3.0-pre-
      connecting to: test
      > var o = new ObjectId()
      > o
      ObjectId("5071731713a0d4c2ef2a2c02")
      > o.str
      5071731713a0d4c2ef2a2c02
      > o.valueOf()
      5071731713a0d4c2ef2a2c02
      > o.toString()
      ObjectId("5071731713a0d4c2ef2a2c02")
      > o.tojson()
      ObjectId("5071731713a0d4c2ef2a2c02")
      > typeof o
      object
      > typeof o.str
      string
      > typeof o.valueOf()
      string
      > typeof o.toString()
      string
      > typeof o.tojson()
      string
      > o.valueOf
      function () {
          return this.str;
      }
      > o.toString
      function () {
          return "ObjectId(" + tojson(this.str) + ")";
      }
      > o.tojson
      function () {
          return this.toString();
      }
      >

      The short version is that any code using ObjectId().toValue() or ObjectId().toString() will break when moving from version 2.0 to version 2.2.

      If compatibility between versions 2.0 and 2.2 is required, use ObjectId().str, which is a string property holding the hex string value in both versions, or use ObjectId().tojson(), which returns a string containing 'ObjectId("<hex string value>")' in both versions.

      Alternatively, if changing affected code is not feasible, it is possible to revert the methods in 2.2 to their 2.0 meanings by changing ObjectId.prototype:

      revertObjectIdMethods.js

      // Revert ObjectId().toString(), .valueOf() and .tojson() to their version 2.0 behaviors
      //
      ObjectId.prototype.toString = function(){
          return this.str;
      }
      ObjectId.prototype.valueOf = function(){
          return "ObjectId(" + tojson(this.str) + ")";
      }
      ObjectId.prototype.tojson = function(){
          return "ObjectId(" + tojson(this.str) + ")";
      }

      Example of using revertObjectIdMethods.js

      MongoDB shell version: 2.3.0-pre-
      connecting to: test
      > var o = new ObjectId()
      > o.valueOf()
      5071ad1b4ed39323ce5d275f
      > o.toString()
      ObjectId("5071ad1b4ed39323ce5d275f")
      > o.tojson()
      ObjectId("5071ad1b4ed39323ce5d275f")
      > load("revertObjectIdMethods.js")
      > o.valueOf()
      ObjectId("5071ad1b4ed39323ce5d275f")
      > o.toString()
      5071ad1b4ed39323ce5d275f
      > o.tojson()
      ObjectId("5071ad1b4ed39323ce5d275f")
      >

      See linked tickets for more information.

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            tad Tad Marshall
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              11 years, 17 weeks, 6 days ago