开发者

how to use "find" to search "_id => OBjectID("id")" in Perl API

开发者 https://www.devze.com 2023-02-03 10:45 出处:网络
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.

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.

I'm trying to do it (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,

$mongo->my_db->my_collection(find({_id => "ObjectId(4d2a0fae9e0a3b4b32f70000"}));

Does not work because "ObjectId" is not a string, but if you do,

./mongo
use my_db
db.my_collection.find({_id : "ObjectId(4d2a0fae9e0a3b4b32f70000)"})

Does not work too, I'm guess Perl API are doing it ^

Now,开发者_StackOverflow I have to know how I do it:

db.my_collection.find({_id : ObjectId("4d2a0fae9e0a3b4b32f70000")})

using Perl API.


The implementation seems changed.

$mongo->my_db->my_collection(
  find({ _id => MongoDB::OID->new(value => "4d2a0fae9e0a3b4b32f70000")})
);


I found the solution, you have to do:

$mongo->my_db->my_collection(find({ _id => $mongo->oid("4d2a0fae9e0a3b4b32f70000")}));


In reading the 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:

use MongoDB;
use MongoDB::OID;

Then you can just do this in your code:

$db->$collection->find_one({ _id => $id })
0

精彩评论

暂无评论...
验证码 换一张
取 消