在MongoDB中使用$ all使用多个$ elemMatch表达式来匹配数组值? [英] Multiple $elemMatch expressions for matching array values using $all in MongoDB?

查看:439
本文介绍了在MongoDB中使用$ all使用多个$ elemMatch表达式来匹配数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题我找到了一种使用$ elemMatch搜索数组值的有趣解决方案.

In the answer to a question I found a interesting solution for searching array values using $elemMatch.

如果我们的收藏夹中包含以下文档:

If we have the following documents in our collection:

{
    foo : [ { bar : "xy", baz : 1 },
            { bar : "a", baz : 10 } ]
},
{
    foo : [ { bar : "xy", baz : 5 },
            { bar : "b", baz : 50 } ]
}

以下查询将仅匹配第一个文档:

The following query will match only the first document:

db.test.find({
    foo : { "$all" : [ { "$elemMatch" : { bar : "xy", baz : 1} }, { "$elemMatch" : { bar : "a", baz : 10 } } ] }
});

我用其他几个示例进行了尝试,它确实有效.但是 $ all 运算符的官方文档没有谈谈结合这两个查询的任何事情.

I tried it with several other examples and it really works. But the official documentation for $all operator doesn't say anything about combining these two queries.

这是预期的行为还是错误?还是这只是文档未涵盖此用例的问题?

Is this the intended behavior or a bug? Or is this just a problem that the documentation does not cover this use case?

推荐答案

这是预期的行为.文档没有涵盖此用例,我们正在努力使其变得更好.但是,很难记录每个可能的查询组合.

This is the intended behavior. The documentation doesn't cover this use case and we are working on it to make it better. Its difficult, however, to document every possible query combination.

这篇关于在MongoDB中使用$ all使用多个$ elemMatch表达式来匹配数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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