Java – find by object ID in Jongo

I know this question is very basic... I'm sorry

I can't use Jongo to make an 'ID by ID' for Mongo

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();

As in the documentation, I can't even compile it... There are two possible objectid types

de.undercouch.bson4jackson.types.ObjectId;

tell me

The constructor ObjectId(String) is undefined

If I use

org.bson.types.ObjectId;

It seems to work better sometimes - but it still tells me that withoid (objectid) is undefined This is not surprising. What object should be part of the function?

My question: how to pass in Jongo_ ID?

Solution

Someone helped me find the answer elsewhere and put it here for future generations

An effective structure is

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

Use org bson. types. ObjectId

or

db.getCollection("mongoTest")
  .findOne(Oid.withOid("5194d46bdda2de09c656b64b"))
  .as(MongoTest.class);`
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>