1

How to convert following query to java code using mongodb QueryBuilder?

db.event.find({"_id":ObjectId("55aff4883e688fbea60d0698")})

2 Answers 2

2

Try this:

DBObject query = QueryBuilder.start("_id").is(ObjectId("55aff4883e688fbea60d0698")).get();
Sign up to request clarification or add additional context in comments.

1 Comment

Gives an Error "The method ObjectId(String) is undefined for the type QuerryTest"
0
//set up DB

//select the collection
DBCollection coll = db.getCollection("event");

//Retrieve
DBCursor cursor = coll.find(QueryBuilder.start().put("_id")
                    .is("55aff4883e688fbea60d0698").get());

while(cursor.hasNext()) {
    System.out.println(cursor.next());
}

Hope it works for you.

3 Comments

What did you get in output? What version of mongodb and java are you using?
I got nothing as the output maybe due to "_id" type is ObjectId not a String. When I ask this question I was working with 2.0 and recently moved to 3.0 it seems more convenient with filters.
Okay, check that the object is in the DB. If you post your code, we could debug it faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.