Perl Mongo找到对象Id

use*_*346 3 perl mongodb

你会认为这是一件简单的事情.我有一个在我的集合中的对象ID列表.我想基于对象ID获得单个记录.谷歌搜索,但没有任何帮助.

所以我有对象ID: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;
Run Code Online (Sandbox Code Playgroud)

这打印:

$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' );
Run Code Online (Sandbox Code Playgroud)

我尝试了上述不同的版本.所有这些都无法编译:

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

$mongo->my_db->my_collection(
Run Code Online (Sandbox Code Playgroud)

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

他们中没有人工作.如何使用对象ID找到(找到)单个记录?

use*_*198 6

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;
Run Code Online (Sandbox Code Playgroud)