Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

perl

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, I have to know how I do it:

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

using Perl API.

like image 854
Mantovani Avatar asked Jan 11 '11 15:01

Mantovani


2 Answers

The implementation seems changed.

$mongo->my_db->my_collection(
  find({ _id => MongoDB::OID->new(value => "4d2a0fae9e0a3b4b32f70000")})
);
like image 121
Weiyan Avatar answered Nov 15 '22 16:11

Weiyan


I found the solution, you have to do:

$mongo->my_db->my_collection(find({ _id => $mongo->oid("4d2a0fae9e0a3b4b32f70000")}));
like image 32
Mantovani Avatar answered Nov 15 '22 15:11

Mantovani