mongodb 如何使用“查找”在 Perl API 中搜索“_id => OBjectID("id")”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4659437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use "find" to search "_id => OBjectID("id")" in Perl API
提问by Mantovani
I have to find a kind of "_id" in my Mongo, I can do it using the Mongo shell, and I can not do that using Perl API.
我必须在我的 Mongo 中找到一种“_id”,我可以使用 Mongo shell 来做到这一点,而我不能使用 Perl API 来做到这一点。
I'm trying to do it (mongo shell):
我正在尝试这样做(mongo shell):
./mongo
use my_db
db.my_collection.find({_id : ObjectId("4d2a0fae9e0a3b4b32f70000")})
It works!(returns), but I can't do that using Perl API,
它有效!(返回),但我不能使用 Perl API 做到这一点,
$mongo->my_db->my_collection(find({_id => "ObjectId(4d2a0fae9e0a3b4b32f70000"}));
Does not work because "ObjectId" is not a string, but if you do,
不起作用,因为“ObjectId”不是字符串,但如果你这样做,
./mongo
use my_db
db.my_collection.find({_id : "ObjectId(4d2a0fae9e0a3b4b32f70000)"})
Does not work too, I'm guess Perl API are doing it ^
也不起作用,我猜 Perl API 正在这样做 ^
Now, I have to know how I do it:
现在,我必须知道我是如何做到的:
db.my_collection.find({_id : ObjectId("4d2a0fae9e0a3b4b32f70000")})
using Perl API.
使用 Perl API。
采纳答案by Weiyan
The implementation seems changed.
实现似乎改变了。
$mongo->my_db->my_collection(
find({ _id => MongoDB::OID->new(value => "4d2a0fae9e0a3b4b32f70000")})
);
回答by Mantovani
I found the solution, you have to do:
我找到了解决方案,你必须这样做:
$mongo->my_db->my_collection(find({ _id => $mongo->oid("4d2a0fae9e0a3b4b32f70000")}));
回答by lhagemann
In reading the MongoDB::Tutorial
在阅读 MongoDB::Tutorial
use MongoDB loads most of the packages you'll need to interact with MongoDB: MongoDB::Connection, MongoDB::Database, MongoDB::Collection, and MongoDB::Cursor. To use special Mongo data types (see MongoDB::DataTypes), you have to include them separately. So, usually, to use Mongo, you'll start with at least:
使用 MongoDB 加载与 MongoDB 交互所需的大部分包:MongoDB::Connection、MongoDB::Database、MongoDB::Collection 和 MongoDB::Cursor。要使用特殊的 Mongo 数据类型(请参阅 MongoDB::DataTypes),您必须单独包含它们。因此,通常,要使用 Mongo,您至少要从以下几点开始:
use MongoDB;
use MongoDB::OID;
Then you can just do this in your code:
然后你可以在你的代码中做到这一点:
$db->$collection->find_one({ _id => $id })