【发布时间】:2016-02-25 18:26:58
【问题描述】:
我正在尝试使用“_id”字段从 mongodb 检索记录。但我得到 null 作为输出。
我的代码:
MongoClient mongo = new MongoClient("localhost",27017);
DB db = mongo.getDB("sample");
DBCollection col = db.getCollection("matching");
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId("56cec592fe3fc16f6b564761"));
DBObject dbObj = col.findOne(query);
System.out.println(dbObj); //getting output as null
我在 mongodb 中的记录:
{
"_id" : ObjectId("56cec592fe3fc16f6b564761"),
"name" : "vishal",
"location" : "Delhi",
"Profession" : "SE",
"number" : 2
}
【问题讨论】:
-
只检查数据库名称和集合名称是否正确。即使大小写也很重要。