简体   繁体   English

Perl mongodb检查是否存在

[英]Perl mongodb check if exists

what is the fastest way in perl to check if the document in mongodb exists? 在perl中检查mongodb中的文档是否存在的最快方法是什么?

Is there any faster way than this?: 有没有比这更快的方法?:

my $cursor = $cache->find( { "key" => $key->key } );

if ( $cursor->count ) {
return 1;
}

Using the count method is fine. 使用count方法很好。 To save a bit of code, you might use find_one instead: 为了节省一些代码,您可以改用find_one

return 1 if $cache->find_one( { "key" => $key->key } );

Both of these have about the same amount of network overhead, requiring at least two roundtrips to send the query and retrieve the results from the cursor. 两者都具有大约相同的网络开销,需要至少两次往返来发送查询并从游标检索结果。

You might save yourself a little processing time if you use a command instead. 如果改用命令,则可以节省一些处理时间。 (Under the hood this just does a find_one against the $cmd collection, though.) find_one ,这实际上find_one$cmd集合执行find_one 。)

my $results = $cache->aggregate( [ { '$match' => { key => $cache->key } }, { '$limit' => 1 } ] );
return 1 if @$results;

But I doubt this level of network overhead will make much of a difference. 但是我怀疑这种级别的网络开销会带来很大的不同。 I encourage you to benchmark it under realistic conditions and see what works best. 我鼓励您在实际条件下对其进行基准测试,并查看最有效的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM