$elemmatch 在 MongoDB 中不起作用 [英] $elemmatch not working in MongoDB

查看:54
本文介绍了$elemmatch 在 MongoDB 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在 mongodb 2.6.1 版中使用以下查询来过滤 mongo 数据,但出现错误.

I am trying to filter mongo data by using the below query in mongodb version 2.6.1 but getting error.

MongoDB 版本 2.4.6(工作):

MongoDB version 2.4.6 (Working):

> db.BC_1839.find({data: {$elemMatch:{$where : "this.First_name.toLowerCase().indexOf('kim') ==0"}}});

输出:

{ 
    "_id" : ObjectId("53719a9d5b9e5c8c110001b9"),
    "data" : [
        {
            "First_name" : "Kimberely",
            "Last_name" : "Weyman",
            "Company_name" : "Scientific Agrcltl Svc Inc",  
            "Address" : "7721 Harrison St",
            "City" : "Kingsway West",
            "State" : "NS",
            "Post" : "2208",   
            "Phone1" : "02-7091-8948",   
            "Phone2" : "0441-151-810", 
            "Email" : "kweyman@weyman.com.au",
            "Web" : "http://www.scientificagrcltlsvcinc.com.au", 
            "active" : "true"
        } 
    ], 
    "history" : [    
        { 
            "timestamp" : "2014-05-13 06:07:55",
            "event": "creation",
            "createdby" : "Srikesh Infotech",
            "creation_data" : [
                {
                    "crm_base_contact_id" : "1839",
                    "crm_imported_files_id" : "1464"
                }  
            ] 
        },
        {
            "timestamp" : "2014-05-13 06:09:05",
            "event" : "Task",
            "createdby" : "Srikesh Infotech",
            "Task_data" : [ 
                {
                    "Campaign ID" : "193",  
                    "Campagin Name" : "Test Campa1"
                }
            ]
        }
    ], 
    "ref" : [ 
        { "crm_base_contact_id" : "1839", "crm_imported_files_id" : "1464" }
    ]
}

MongoDB 2.6.1 版(不工作):

MongoDB version 2.6.1(Not Working):

> db.BC_1839.find({data: {$elemMatch:{$where : "this.First_name.toLowerCase().indexOf('kim') ==0"}}});

输出:

error: {
        "$err" : "Can't canonicalize query: BadValue $elemMatch cannot contain $
where expression",
        "code" : 17287
}

同样的查询在 mongodb 2.4.6 版中执行但在 mongodb 2.6.1 版中不执行为什么???

Same query executes in mongodb version 2.4.6 but not in mongodb version 2.6.1 Why???

推荐答案

它根本不应该在早期版本中工作,因为至少你现在已经修改了 this 的范围将数据"称为顶级元素.简而言之,这不再被允许,除非绝对必要,否则您真的不应该使用 JavaScript 方法.即便如此,在大多数情况下可能仍然有更好的方法.

It shouldn't have worked in earlier versions at all, as at the very least you have modified the scoping of this to now refer to "data" as a top level element. In short, this is no longer allowed and you really should not be using JavaScript methods unless you absolutely have to. Even then, there is probably still a better way in most cases.

但实际上这是对 JavaScript 匹配的不必要使用,因为当存在其他运算符时不需要这样做.

But in fact this is an un-necessary use of JavaScript matching as it is not required when there are other operators existing that will do this.

您应该使用 $regex 形式:

db.docs.find({ "data.First_name": /^kim/i })

或字段内的任何地方,删除插入符号^:

Or anywhere within the field, remove the caret ^:

db.docs.find({ "data.First_name": /kim/i })

这几乎和 JavaScript 执行一样低效,但没有通过解释器引擎处理的开销那么低.当然,它适用于任何地方.

Which is pretty much as inefficient as JavaScript execution but not as much as there is not the overhead of processing through that interpreter engine. And of course it works everywhere.

还要考虑一下依赖 JavaScript 来解析的查询实际上在做什么:

Also think about what a query relying on JavaScript to resolve is actually doing:

  • 调用 JavaScript 解释器实例
  • 将每个文档的 BSON 文档类型转换为 JavaScript 类型
  • 在每个文档的解释器中评估 JavaScript 代码
  • 转换 JavaScript true|false 作为每个文档的结果
  • Invokes a JavaScript interpreter instance
  • Converts BSON document types per document to JavaScript types
  • Evaluates JavaScript code in the interpreter per document
  • Casts JavaScript true|false back as a result per document

考虑到 $regex(但不区分大小写的匹配不是最佳的)正在执行相同的操作,但本机使用pcre"C 库,无需转换和重铸每个文档,那么这显然是两者的明智选择.

Considering that $regex ( but with a case insensitive match which is not optimal ) is doing the same operations but using the "pcre" C library natively without conversion and recasting per document, then it is clearly the sane choice of the two.

这篇关于$elemmatch 在 MongoDB 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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