在一个mongodb的$匹配,如何测试场匹配,而不是现场EQUALLING [英] Within a mongodb $match, how to test for field MATCHING , rather than field EQUALLING

查看:112
本文介绍了在一个mongodb的$匹配,如何测试场匹配,而不是现场EQUALLING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在$匹配阶段加入汇聚管道筛选,其中一个字段匹配的查询时,(和它可能有其他的数据也是如此),而不是限制结果条目,其中外地等于查询?

Can anyone tell me how to add a $match stage to an aggregation pipeline to filter for where a field MATCHES a query, (and may have other data in it too), rather than limiting results to entries where the field EQUALS the query?

查询规范...

var query = {hello:"world"};

...可以用来检索使用MongoDB的本地节点的驱动程序,该查询'地图'是间preTED为匹配...

...can be used to retrieve the following documents using the find() operation of MongoDb's native node driver, where the query 'map' is interpreted as a match...

{hello:"world"}
{hello:"world", extra:"data"}

...喜欢...

...like...

collection.find(query);

与$ elemMatch用于检索匹配包含在这样的文件阵列条目文档时,同样的查询地图也可作为PTED比赛间$ P $ ...

The same query map can also be interpreted as a match when used with $elemMatch to retrieve documents with matching entries contained in arrays like these documents...

{
  greetings:[
    {hello:"world"},
  ]
}

{
  greetings:[
    {hello:"world", extra:"data"},
  ]
}

{
  greetings:[
    {hello:"world"},
    {aloha:"mars"},
  ]
}

使用类似[PIPELINE1]调用

...

...using an invocation like [PIPELINE1] ...

collection.aggregate([
  {$match:{greetings:{$elemMatch:query}}},
]).toArray()

然而,试图获得与匹配的问候名单平仓[PIPELINE2] ...

However, trying to get a list of the matching greetings with unwind [PIPELINE2] ...

collection.aggregate([
  {$match:{greetings:{$elemMatch:query}}},
  {$unwind:"$greetings"},
]).toArray()

...产生具有任何匹配的条目的文件内的所有数组项,其中包括不匹配(简化结果)...

...produces all the array entries inside the documents with any matching entries, including the entries which don't match (simplified result)...

[
  {greetings:{hello:"world"}},
  {greetings:{hello:"world", extra:"data"}},
  {greetings:{hello:"world"}},
  {greetings:{aloha:"mars"}},
]

我一直在试图添加第二个比赛阶段,但我很惊讶地发现,它的成果有限,只有那些那里的问候字段等于查询,而不是它匹配的查询[PIPELINE3]

I have been trying to add a second match stage, but I was surprised to find that it limited results only to those where the greetings field EQUALS the query, rather than where it MATCHES the query [PIPELINE3].

collection.aggregate([
  {$match:{greetings:{$elemMatch:query}}},
  {$unwind:"$greetings"},
  {$match:{greetings:query}},
]).toArray()

不幸的是PIPELINE3只产生以下条目,的不包括的匹配的Hello World条目以额外的数据,因为该条目是不严格平等来查询(简化的结果)..

Unfortunately PIPELINE3 produces only the following entries, excluding the matching hello world entry with the extra:"data", since that entry is not strictly 'equal' to the query (simplified result)...

[
  {greetings:{hello:"world"}},
  {greetings:{hello:"world"}},
]

...这里是我需要的结果是相当...

...where what I need as the result is rather...

[
  {greetings:{hello:"world"}},
  {greetings:{hello:"world"}},
  {greetings:{"hello":"world","extra":"data"}
]

我怎么可以添加第二个$比赛阶段PIPELINE2,为那里的问候场相匹配的查询,过滤器(可能有它的其他数据也是如此),而不是限制结果条目时的问候语字段等于查询?

How can I add a second $match stage to PIPELINE2, to filter for where the greetings field MATCHES the query, (and may have other data in it too), rather than limiting results to entries where the greetings field EQUALS the query?

推荐答案

你们看到的结果是正确的。你的做法是有点不对劲。如果你想你期待的结果,那么你应该使用这个方法:

What you're seeing in the results is correct. Your approach is a bit wrong. If you want the results you're expecting, then you should use this approach:

collection.aggregate([
  {$match:{greetings:{$elemMatch:query}}},
  {$unwind:"$greetings"},
  {$match:{"greetings.hello":"world"}},
]).toArray()

有了这个,你应该得到的输出如下:

With this, you should get the following output:

[
  {greetings:{hello:"world"}},
  {greetings:{hello:"world"}},
  {greetings:{"hello":"world","extra":"data"}
]

每当你使用汇聚 MongoDB中,并希望建立一个聚合管道能够产生你所期望的文件,你应该总是先从第一阶段查询。然后最终添加阶段来监测从后续级的输出。

Whenever you're using aggregation in MongoDB and want to create an aggregation pipeline that yields documents you expect, you should always start your query with the first stage. And then eventually add stages to monitor the outputs from subsequent stages.

$放松级的输出是:

[{
  greetings:{hello:"world"}
},
{
  greetings:{hello:"world", extra:"data"}
},
{
  greetings:{hello:"world"}
},
{
  greetings:{aloha:"mars"}
}]

现在,如果我们包括您所使用的第三个阶段,那么它会匹配问候具有值 {打招呼:世界 } 并与精确值,它会发现只有两个管道的文件。所以,你只会越来越:

Now if we include the third stage that you used, then it would match for greetings key that have a value {hello:"world"} and with that exact value, it would find only two documents in the pipeline. So you would only be getting:

{ "greetings" : { "hello" : "world" } }
{ "greetings" : { "hello" : "world" } }

这篇关于在一个mongodb的$匹配,如何测试场匹配,而不是现场EQUALLING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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