网上找了很久查询objectid的方法都是错的,用mongovue能查询出来,但就是用java不知道怎么查询
1.mongovue里的查询方式:
{"_id" : ObjectId("5326bfc0e6f780b21635248f")}
2.纯mongodb里的查询方式:
db.collect.find({ "_id" : ObjectId("5326bfc0e6f780b21635248f") })
3.java里的查询方式:
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; }
从外国网站找过来的,链接:http://stackoverflow.com/questions/9797935/how-to-query-documents-using-id-field-in-java-mongodb-driver