MongoDB - $setIsSubset 运算符不适用于 $match 阶段 [英] MongoDB - $setIsSubset operator not working with $match stage

查看:47
本文介绍了MongoDB - $setIsSubset 运算符不适用于 $match 阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构造了一个这样的查询:

I constructed a query like this:

db.test.aggregate([{$match: {$setIsSubset: [['hello', 'you'], '$words']}}])

我想返回字段 'words' 的数组包含字符串 ['hello', 'you'] 的所有文档.

I want to return all documents where the array of the field 'words' contains the strings ['hello', 'you'].

执行此查询时出现此错误:

Executing this query i get this error:

"errmsg" : "未知的顶级运算符:$setIsSubset"

"errmsg" : "unknown top level operator: $setIsSubset"

我做错了什么?

感谢您的帮助!

推荐答案

你应该使用 $expr 如果你想在 $match 中使用 $setIsSubset(这是一个表达式):

You should use $expr if you want to use $setIsSubset (which is an expresion) inside $match:

db.test.aggregate([
    {
        $match: {
            $expr: {
                $setIsSubset: [["hello", "you"], "$words"]}
            }
        }
])

对于低于 3.6 的 MongoDB 版本,您可以使用 $编辑:

For MongoDB versions lower than 3.6 you can use $redact:

db.test.aggregate([
    {
        $redact: {
            $cond: {
                if: { $eq: [ { $setIsSubset: [["hello", "you"], "$words"]}, true ] },
                then: "$$KEEP",
                else: "$$PRUNE"
            }
        }
    }
])

这篇关于MongoDB - $setIsSubset 运算符不适用于 $match 阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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