$in mongoDB 运算符,perl 中带有 _id [英] $in mongoDB operator with _id in perl

查看:50
本文介绍了$in mongoDB 运算符,perl 中带有 _id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 MongoDB 数据库上使用 perl 执行此查询:

I try to execute this query with perl on a MongoDB database :

$db->$collection->find({"_id" : { "$in" : ["4f520122ecf6171327000137", "4f4f49c09d1bd90728000034"]}});

但它什么都不返回,它必须返回两个文档.这个查询有什么问题?

But it return nothing and it must return two documents. What is wrong with this query ?

谢谢.

它也不起作用:

$db->$collection->find( {_id => "4f520122ecf6171327000137"} );

推荐答案

首先,确保您使用正确的语法.您的第一个示例不是有效的 Perl 代码,因为您将一大块 JSON 作为查询参数包含在内.

First, make sure you're using the correct syntax. Your first example is not valid Perl code, since you're including a chunk of JSON as the query parameter.

其次,假设这些 ID 值是 MongoDB ObjectID,您需要创建 OID 对象以将它们与普通字符串区分开来.并确保在 $in 周围使用单引号 (''),否则 Perl 会尝试将 $in 插入为变量(大概是里面什么都没有).

Second, assuming these ID values are MongoDB ObjectID's, you'll need to make OID objects in order to differentiate them from ordinary strings. And make sure to use single quotes ('') around $in, otherwise Perl will try to interpolate $in as a variable (which presumably has nothing in it).

所以我假设你想做这样的事情:

So I assume you want to do something like this:

$db->$collection->find( {
    "_id" => { 
        '$in' => [ MongoDB::OID->new( value => "4f520122ecf6171327000137" ), 
                   MongoDB::OID->new( value => "4f4f49c09d1bd90728000034" )
                 ]
             } 
} );

此外,使用自动加载的方法名称来检索集合已被弃用一段时间.你最好使用 $db->get_collection( "collection name" )->find( ... )

Additionally, using autoloaded method names to retrieve collections has been deprecated for a while. You're better off using $db->get_collection( "collection name" )->find( ... )

这篇关于$in mongoDB 运算符,perl 中带有 _id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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