Perl Mongo 查找对象 ID [英] Perl Mongo find object Id

查看:35
本文介绍了Perl Mongo 查找对象 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

You would think it is a simple thing. I have a list of object id's that are in my collection. I would like to get a single record based on an object id. Have Googled, but nothing helpful.

所以我有对象 ID:5106c7703abc120a04070b34

So I have object 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' );

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

I have tried different verions of the above find. All of them fail to compile:

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

$mongo->my_db->my_collection(

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

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

它们都不起作用.如何使用对象 ID 查找(findone)单个记录??

NONE of them work. How do I find (findone) a single record using the object id??

推荐答案

find 方法返回一个用于迭代的 Cursor 对象.如果您只想要一条记录,请使用返回值的 find_one 方法.

the find methods returns a Cursor object for iterating through. If you only want one record use the find_one method which returns a value.

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;

这篇关于Perl Mongo 查找对象 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆