0

I try to execute this query with perl on a MongoDB database :

$db->$collection->find({"_id" : { "$in" : ["4f520122ecf6171327000137", "4f4f49c09d1bd90728000034"]}});

But it return nothing and it must return two documents. What is wrong with this query ?

Thank you.

Edit : It doesn't work too :

$db->$collection->find( {_id => "4f520122ecf6171327000137"} );
Toin3
  • 49
  • 1
  • 5
  • 1
    To help us answer you better, you should've shown us the query working correctly in the mongo shell, and proved that you have two documents with the given ObjectIds. – A. Jesse Jiryu Davis Jan 31 '13 at 21:56

1 Answers1

16

First, make sure you're using the correct syntax. Your first example is not valid Perl code, since you're including a chunk of JSON as the query parameter.

Second, assuming these ID values are MongoDB ObjectID's, you'll need to make OID objects in order to differentiate them from ordinary strings. And make sure to use single quotes ('') around $in, otherwise Perl will try to interpolate $in as a variable (which presumably has nothing in it).

So I assume you want to do something like this:

$db->$collection->find( {
    "_id" => { 
        '$in' => [ MongoDB::OID->new( value => "4f520122ecf6171327000137" ), 
                   MongoDB::OID->new( value => "4f4f49c09d1bd90728000034" )
                 ]
             } 
} );

Edit: Additionally, using autoloaded method names to retrieve collections has been deprecated for a while. You're better off using $db->get_collection( "collection name" )->find( ... )

friedo
  • 65,762
  • 16
  • 114
  • 184
  • Thank you for your answer but I already try and it doesn't work. I think it's a problem with mongoDB driver but I'm not 100% sure :s – Toin3 Jan 31 '13 at 20:22
  • 8
    I seriously doubt it's a problem with the Perl driver since I maintain it. I am able to query ObjectID's with the above syntax and it works as expected. – friedo Jan 31 '13 at 20:44
  • Finaly, it works as you say in your answer, but I don't no why now and not before. – Toin3 Feb 12 '13 at 19:11