【问题标题】:Perl Mongo find object IdPerl Mongo 查找对象 ID
【发布时间】:2014-03-09 16:43:34
【问题描述】:

你会认为这是一件简单的事情。我的收藏中有一个对象 ID 列表。我想根据对象 ID 获取一条记录。用 Google 搜索过,但没有任何帮助。

所以我有对象 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;

打印出来:

$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' );

我已经尝试了上述发现的不同版本。全部编译失败:

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

$mongo->my_db->my_collection(

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

它们都不起作用。如何使用对象 id 找到(findone)单个记录??

【问题讨论】:

    标签: perl mongodb


    【解决方案1】:

    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;
    

    【讨论】:

    • 这也返回相同的错误答案。当我自己连接到 mongo 时,我可以通过 find().pretty() 看到该 ID。当我在 PERL 中执行 find_one 时,我得到的只是:{ '_id' => bless( { 'value' => '51030e053739330d6caa7007' }, 'MongoDB::OID' ) };
    • 已修复,没有注意到你的参数 s 也弄错了。
    • 谢谢!就是这样。不知道我是如何以这种方式结束参数的。
    【解决方案2】:

    这个问题的答案已经改变。 MongoDB::OID 已被弃用,取而代之的是 BSON::OID,它没有允许您传入 24 字节十六进制字符串的方法。以下是您这些天必须做的事情:

    my $id = "5c7463277fc2198b64654feb";
    my $oid = BSON::OID->new(oid => pack('H24', $id));
    my $result = $db->get_collection('mycollection')->find_id($oid);
    

    pack 从 $id 中的 24 字节十六进制数据创建一个 12 字节二进制序列。这是 BSON::OID 所期望的,然后 perl 驱动程序会在后台为您构建正确的过滤器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 2017-05-31
      相关资源
      最近更新 更多