我试图通过 MongoID“_id”字段在 mongoDB 中查找记录。我找到了有关如何执行此操作的示例,但无法使其发挥作用。例子:
$recID = "010101010101011"; //would be a valid mongodb _id
$recID = new MongoId((string)$recID); // I have tried it without the (string) cast too
$cursor = $this->Collection->findOne(array('_id' => $recID));
print_r($cursor);
它输出:
MongoCursor (
)
里面什么也没有。
我已经通过将上面的“_id”更改为不同的字段(例如“firstName”)并传入名字来验证其他所有内容都正常工作,然后我得到了有效数据。
为什么这不起作用?
我什至尝试使用 $recID 作为字符串进行搜索,没有区别。
这是 mongo shell 中发生的情况(尽管我不确定我是否正确查询):
>
> db.Employee.find({login:"myperson"})
{ "_explicitType" : "project.Employee", "_id" : ObjectId("4e209564203d83940f0000
06"), "active" : true, "addedDate" : "07/15/2011 15:29:21", "domain" : "xxx",
"id" : ObjectId("4e209564203d83940f000006"), "lastLogin" : "07/20/2011 19:13:36
", "login" : "myperson", "name" : "My Person", "pw" : "", "ulevel" : 9999
}
> db.Employee.find({id:"4e209564203d83940f000006"})
> db.Employee.find({_id:"4e209564203d83940f000006"})
>
请注意,没有为 id 或 _id 返回任何内容。