[JAVA-85] CompareTo of ObjectId java driver returns different value than MongoDB implementation Created: 01/Feb/10  Updated: 14/May/14  Resolved: 30/Jan/11

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.5

Type: Bug Priority: Major - P3
Reporter: Dan Levison Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux, Jboss


Backwards Compatibility: Minor Change

 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



 Comments   
Comment by auto [ 03/Mar/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-85: removed trick to compare inc wrap
https://github.com/mongodb/mongo-java-driver/commit/170127bdb63e9c7b4dded9f3add7d670935d53cd

Comment by auto [ 03/Mar/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-85: refactoring of ObjectId, to store values native, and encode/decode big endian. Fixes some flaws with Increment value failing comparison.
https://github.com/mongodb/mongo-java-driver/commit/b7d9986e5ac45370e77797d922bb844cf8e05ffb

Comment by auto [ 30/Jan/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

Message: fix ObjectId.compareTo JAVA-85
https://github.com/mongodb/mongo-java-driver/commit/04aa553e59dc1b1961198ed9235573653dff8aef

Generated at Thu Feb 08 08:51:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.