Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1785

ObjectId.compareTo() returns wrong response

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.0.1
    • Affects Version/s: 3.0.0
    • Component/s: BSON
    • Environment:
      JDK 7.0_51
    • Major Change

      I have 2 long values [100, 200], with which I create two ObjectId objects, using a byte array
      byte1 = byte [] bytes = ByteBuffer.allocate(12).putLong(value1).put(new byte[4]).array()
      objectId1 = ObjectId(byte1)

      This constructs the following two ObjectIds:
      obj1 = 000000000000006400000000
      obj2 = 00000000000000c800000000

      When i try to compare: obj1.compareTo(obj2)<0 the result is false
      This happens because obj1's processIdentifier value (bytes "6400") is set to value "25600", while the obj2's processIdentifier value (bytes "c800") is set to the signed "-14336".
      So the comparison "x = processIdentifier - other.processIdentifier;" is positive and obj1.compareTo(obj2)<0 returns false.

      In v2.x.x versions of the driver, there was a "_compareUnsigned" operation, for handling these comparisons.

      Is this a bug or this is the way it should work now?

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            pkranas Pavlos Kranas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: