I have been looking for a long time to find the wrong way to query objectid. I can query it using mongovue, but I don’t know how to query it using java.
1. Query method in mongovue:
Copy the code code as follows:
{"_id" : ObjectId("5326bfc0e6f780b21635248f")}
2. Query method in pure mongodb:
Copy the code code as follows:
db.collect.find({ "_id" : ObjectId("5326bfc0e6f780b21635248f") })
3.Query method in java:
Copy the code code as follows:
import org.bson.types.ObjectId;
public DBObject findDocumentById(String id) {
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id));
DBObject dbObj = collection.findOne(query);
return dbObj;
}