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

CompareTo of ObjectId java driver returns different value than MongoDB implementation

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.5
    • Component/s: None
    • Labels:
      None
    • Environment:
      Linux, Jboss
    • Backwards Compatibility:
      Minor Change
    • # Replies:
      3
    • Last comment by Customer:
      true

      Description

      I'am sorting messages in my program according to the ObjectId and than trying to fetch messages from MongoDB with ID bigger than the one I have, but than I'm getting messages I already have.
      After some tests and reading the documentation it looks like the Java ObjectId / compareTo is wrong.
      My test:
      @Test
      public void compareMongoToMongo2()

      { final String MONGO_ID_1 = "4b5d4825f5f32ad607e4c7c7"; final String MONGO_ID_2 = "4b5d4825f5f32ad6eae3c7c7"; ObjectId id1 = new ObjectId(MONGO_ID_1); ObjectId id2 = new ObjectId(MONGO_ID_2); System.out.println(id1.compareTo(id2)); }

      output: 29
      Meaning MONGO_ID_1 > MONGO_ID_2

      But when running the following query on MongoDB:
      > db.messages.find({"_id": {$gt: ObjectId("4b5d4825f5f32ad607e4c7c7")}} ,

      {"_id":1}

      )

      { "_id" : ObjectId("4b5d4825f5f32ad6eae3c7c7") }

      Meaning MONGO_ID_2 > MONGO_ID_1

        Attachments

          Activity

            People

            • Votes:
              3 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                6 years, 30 weeks, 2 days ago
                Date of 1st Reply: