$elemmatch 在 MongoDB 中不起作用

编程入门 行业动态 更新时间:2024-10-24 16:33:35
本文介绍了$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.au", "Web" : "www.scientificagrcltlsvcinc.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 中不起作用

本文发布于:2023-11-22 13:05:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   elemmatch   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!