3

I know this question is incredibly basic... I'm sorry in advance.

I can't do a 'find by ID' for Mongo using Jongo.

I tried

Iterator<MongoTest> all = db.getCollection("mongoTest").find("{'_id': ObjectId('5194d46bdda2de09c656b64b')}").as(MongoTest.class).iterator();

Error:

java.lang.IllegalArgumentException: {'_id': ObjectId('5194d46bdda2de09c656b64b')} cannot be parsed
at org.jongo.query.JsonQuery.marshallQuery(JsonQuery.java:34)
at org.jongo.query.JsonQuery.<init>(JsonQuery.java:27)
at org.jongo.query.JsonQueryFactory.createQuery(JsonQueryFactory.java:52)
at org.jongo.Find.<init>(Find.java:41)
at org.jongo.MongoCollection.find(MongoCollection.java:79)
at org.jongo.MongoCollection.find(MongoCollection.java:75)

I tried

Iterator<MongoTest> all = db.getCollection("mongoTest").find(withOid(new ObjectId("5194d46bdda2de09c656b64b"))).as(MongoTest.class).iterator();

exactly as in the documentation, and I can't even get it to compile ... there are two possible types of ObjectId.

de.undercouch.bson4jackson.types.ObjectId;

Tells me

The constructor ObjectId(String) is undefined

And if I use

org.bson.types.ObjectId;

it seems to work better, sometimes - but it still tells me that withOid( ObjectId ) is undefined. Which isn't entirely surprising, cause exactly what object is that function supposed to be part of?

My question: How do I do a find by _id in Jongo?

Wolfman Joe
  • 799
  • 1
  • 8
  • 23
  • IMO Jongo was incredibly difficult to get started with and the API was pretty painful to work with. Not a great answer to your question, but I'd take a look at some other projects for accessing Mongo via Java. I'd highly recommend [SpringData Mongo](http://www.springsource.org/spring-data/mongodb) See [this question](http://stackoverflow.com/questions/16046852/access-mongodb-from-java/16047355#16047355) for some decent alternatives. – David Welch May 16 '13 at 15:14
  • Thank you. I do not mind being offered alternatives. We're still experimenting with solutions to our need for a docdb. – Wolfman Joe May 16 '13 at 15:32
  • It depends on what you want or need, but jongo is not "incredibly difficult to get started with". Admittedly, you don't have access to as many options as with the basic mongo Java driver, but nothing prevents you from using them together. – assylias May 16 '13 at 16:46

1 Answers1

8

Someone helped me to find an answer elsewhere, putting it here for posterity

A valid construction for this is

db.getCollection("mongoTest")
  .find("{ _id: # }", new ObjectId("5194d46bdda2de09c656b64b"))
  .as(MongoTest.class);

Using org.bson.types.ObjectId

or

db.getCollection("mongoTest")
  .findOne(Oid.withOid("5194d46bdda2de09c656b64b"))
  .as(MongoTest.class);`
davnicwil
  • 28,487
  • 16
  • 107
  • 123
Wolfman Joe
  • 799
  • 1
  • 8
  • 23
  • 1
    withOid() uses a String as parameter. In case of doubt on a method, your IDE completion can be helpful :D collection.findOne(Oid.withOid("5194d46bdda2de09c656b64b")).as(Friend.class) – yves amsellem May 16 '13 at 22:55
  • @yvesamsellem `Oid.withOid()` - the Oid. was missing from the documentation. Thank you! Hard to IDE-complete when you don't have the object to work with. – Wolfman Joe May 17 '13 at 12:27
  • 1
    `Oid` is a static import. In the documentation, look at the imports :D – yves amsellem May 17 '13 at 12:37
  • 1
    Ohhhhhh, I seeeeeee. (facepalms) Thank you very much. – Wolfman Joe May 17 '13 at 12:55
  • Old question but thought I would reply for sake of others on Google. You have to use Oid.withOid(), which returns the following query parameter internally - "{_id: {$oid:\"" + id + "\"}}". This was the only way I could get it to work within queries and updates by ids. – sandman Oct 14 '16 at 13:27