为什么我无法在mongodb中找到_id的记录 [英] Why am I unable to find a record by _id in mongodb

查看:181
本文介绍了为什么我无法在mongodb中找到_id的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过它的MongoID_id字段在mongoDB中找到记录。我已经找到了如何做的例子,但不能得到它的工作。示例:

  $ recID =010101010101011; //是一个有效的mongodb _id 
$ recID = new MongoId((string)$ recID); (字符串)强制转换
$ cursor = $ this-> Collection-> findOne(array('_ id'=> $ recID));
print_r($ cursor);

输出:

  MongoCursor(

b
$ b

我已经通过将上面的_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:我的人,pw:,ulevel:9999
}
> db.Employee.find({id:4e209564203d83940f000006})
> db.Employee.find({_ id:4e209564203d83940f000006})
>

注意没有返回id或_id。解决方案试试 db.Employee.find({_ id:ObjectId(4e209564203d83940f000006)}


I am trying to find a record in mongoDB by it's MongoID "_id" field. I have found examples on how to do it, but can not get it to work. Example:

$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);

It outputs:

MongoCursor (
)

Nothing inside.

I have verified everything else is working by changing the "_id" above to a different field such as "firstName" and passing in a first name and I get back valid data.

Why does this not work?

I have even tried searching with $recID as a string, no difference.

Here is what happens from the mongo shell (though I am not sure if I am querying properly):

>
> 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"})
>

Notice nothing returned for id or _id.

解决方案

Try db.Employee.find({_id:ObjectId("4e209564203d83940f000006")}

这篇关于为什么我无法在mongodb中找到_id的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆