Perl mongo find object id

You would think that is easy. I have a list of object identifiers that are in my collection. I would like to get one record based on the identifier of the object. Googled, but nothing useful.

So I have an object identifier: 5106c7703abc120a04070b34

my $client = MongoDB::MongoClient->new;
my $db = $client->get_database( 'myDatabase' );

my $id_find = $db->get_collection('mycollection')->find({},{_id =>      MongoDB::OID->new(value => "5106c7703abc120a04070b34")});

print Dumper $id_find;

Fingerprints:

$VAR1 = bless( {
                 '_skip' => 0,
                 '_ns' => 'MindCrowd_test.Users',
                 '_grrrr' => 0,
                 'partial' => 0,
                 '_query' => {},
                 '_tailable' => 0,
                 '_client' => bless( {
                                       'w' => 1,
                                       'query_timeout' => 30000,
                                       'find_master' => 0,
                                       '_servers' => {},
                                       'sasl' => 0,
                                       'wtimeout' => 1000,
                                       'j' => 0,
                                       'timeout' => 20000,
                                       'sasl_mechanism' => 'GSSAPI',
                                       'auto_connect' => 1,
                                       'auto_reconnect' => 1,
                                       'db_name' => 'admin',
                                       'ssl' => 0,
                                       'ts' => 0,
                                       'inflate_dbrefs' => 1,
                                       'port' => 27017,
                                       'host' => 'mongodb://localhost:27017',
                                       'dt_type' => 'DateTime',
                                       'max_bson_size' => 16777216
                                     }, 'MongoDB::MongoClient' ),
                 '_limit' => 0,
                 'slave_okay' => 0,
                 '_request_id' => 0,
                 'immortal' => 0,
                 'started_iterating' => 0
               }, 'MongoDB::Cursor' );

I tried different variations of the above find. All of them will not compile:

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

$mongo->my_db->my_collection(

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

None of them work. How to find (find) one record using the object identifier?

+3
source share
1 answer

find Cursor . , find_one, .

my $client = MongoDB::MongoClient->new;
my $db = $client->get_database( 'myDatabase' );

my $id_find = $db->get_collection('mycollection')->find_one({_id => MongoDB::OID->new(value => "5106c7703abc120a04070b34")});

print Dumper $id_find;
+6

All Articles