QnaList > Groups > Mongodb-User > Apr 2016
faq

[mongodb-user] MongoDB Java Driver Find() Returns Exploded ObjectId?

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
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 [email protected]
To post to this group, send email to [email protected]
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.

asked Apr 4 2016 at 16:49

=?ISO-8859-1?Q?Jos=E9_Coelho?= 's gravatar image



3 Replies for : [mongodb-user] MongoDB Java Driver Find() Returns Exploded ObjectId?
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
*{* name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0 <http://twitter.com/rossc0>", "@MongoDB
<https://twitter.com/mongodb>"],
  facebook :"MongoDB
<https://www.google.com/url?q=https%3A%2F%2Fwww.facebook.com%2Fmongodb&sa=D&sntz=1&usg=AFQjCNGPMcaFBzmWsh-MpaWeTH6vMQoDIg>
"*}*
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 [email protected]
To post to this group, send email to [email protected]
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/CAOjLY%2BGcR0H1tMw1ECrdSRh2iAPPJ0P-tumbi9njSv6012tptQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

answered Apr 5 2016 at 01:23

Ross Lawley 's gravatar image


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 
ObjectId...
terça-feira, 5 de Abril de 2016 às 09:24:15 UTC+1, Ross Lawley escreveu:
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 [email protected]
To post to this group, send email to [email protected]
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/8e2acba0-87a7-4798-be8d-d586f390d38c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

answered Apr 5 2016 at 05:53

=?ISO-8859-1?Q?Jos=E9_Coelho?= 's gravatar image


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
*{* name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0 <http://twitter.com/rossc0>", "@MongoDB
<https://twitter.com/mongodb>"],
  facebook :"MongoDB
<https://www.google.com/url?q=https%3A%2F%2Fwww.facebook.com%2Fmongodb&sa=D&sntz=1&usg=AFQjCNGPMcaFBzmWsh-MpaWeTH6vMQoDIg>
"*}*
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 [email protected]
To post to this group, send email to [email protected]
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/CAOjLY%2BHnUOaGwo-828ZpfjbwrWHRace%3DikwZfjUZuxuycafsZA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

answered Apr 5 2016 at 06:09

Ross Lawley 's gravatar image


Related discussions

Tagged

Group Mongodb-user

asked Apr 4 2016 at 16:49

active Apr 5 2016 at 06:09

posts:4

users:2

©2013 QnaList.com