MongoDB Java driver find() returns exploded ObjectId?

253 views
Skip to first unread message

José Coelho

unread,
Apr 4, 2016, 7:58:05 PM4/4/16
to mongodb-user
I'm just doing CRUD on a mongo database with Java.
My problem is when I query the database

db.getCollection("something").find()

I get the documents correctly, but the ObjectId of each document is something like this:

"_id": {
     
"timestamp": 1459641911,
     
"machineIdentifier": 16375804,
     
"processIdentifier": 3908,
     
"counter": 9920756,
     
"time": 1459641911000,
     
"date": "2016-04-03T00:05:11.000+0000",
     
"timeSecond": 1459641911
   
}

I need to get the 12-byte ObjectId, like this:

ObjectId("507f1f77bcf86cd799439011")

How do I do that?

Thank you

Ross Lawley

unread,
Apr 5, 2016, 4:24:15 AM4/5/16
to mongod...@googlegroups.com
Hi José,

Can you provide a little more information.  

* What version of the Java driver are you using? 
* Was it the Java driver that created the ObjectId?
* How are you viewing that ObjectId? (printing it to stdout?)

Cheers,

Ross 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9f051140-79ae-4cbb-9775-ead3b08e533e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

José Coelho

unread,
Apr 5, 2016, 8:53:14 AM4/5/16
to mongodb-user
I'm using mongodb-driver 3.2.2 and I'm implementing a RESTful API with jax-rs.

If I go on mongodb shell and query I get the 12-byte representation, it's only when I output it directly to a Response.entity() that it explodes the ObjectId...

Ross Lawley

unread,
Apr 5, 2016, 9:10:01 AM4/5/16
to mongod...@googlegroups.com
Hi,

Ah ok, it sounds as if the serialization in jax-rs is reflecting the internal values from the ObjectId.  Its not a stack I've personal experience of but I think if you create a custom serializer for ObjectId's it will fix the issue for you.  If you are using Jackson with jax-rs then an example can be found here: https://simplapi.wordpress.com/2013/06/18/handling-mongodb-objectid-threw-jerseyjackson/

All the best,

Ross


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages