0

I am trying to get a single record using the _id field , here is my code

BasicDBObject query = new BasicDBObject("_id","52a6a2cc05e1c80fd5e9295c");

DBCursor cursor = blogTable.find(query);
while (cursor.hasNext())
{
    System.out.println("got u");
    dataText.setText((cursor.next().get("content:encoded")).toString());
}

I simple don't get any data and I am quite sure the id exist .please help

2
  • What happens when you print out just "cursor.next()"? Not sure if that will potentially show you the data or not. Are you getting "got u" to print? Commented Dec 10, 2013 at 19:00
  • Actually i got it I need to pass the object as it is and not convert it to string by using toString method Thanks anyway Commented Dec 10, 2013 at 19:05

1 Answer 1

3

I'm guessing that the String you are calling out as "52a6a2cc05e1c80fd5e9295c" is actually an ObjectID in the MongoDB. If this is the case, your lookup is...

BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId("52a6a2cc05e1c80fd5e9295c"));
DBObject dbo = blogTable.findOne(query);
if ( dbo != null ) {
    System.out.println( "got u " + dbo );
} else {
    System.out.println( "not found" );
}

You need to know the types of things in MongoDB.

Sign up to request clarification or add additional context in comments.

Comments

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.