在 $project 阶段使用 $in 运算符过滤数组 [英] Filter array using the $in operator in the $project stage

查看:10
本文介绍了在 $project 阶段使用 $in 运算符过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,无法使用 $in$filter 数组聚合运算符中的运算符.

假设这是文档架构:

<代码>{_id: 1,用户: [{_id: 'a',帐户:['x','y','z']},{_id: 'b',帐户:['j','k','l']}]}

我想使用 aggregate,根据 accounts 数组的内容获取带有 users 过滤数组的文档.>

IF $in 将与 $filter 运算符一起使用,我希望它看起来像这样:

db.test.aggregate([{$项目:{'filtered_users':{$过滤器:{输入:'$用户',如:'用户',条件: {$in: ['$$user.accounts', ['x']]}}}}}])

并在 filtered_users 中仅返回 x 以来的第一个用户他的帐户.

但是,正如我所说,这不起作用并且我收到错误消息:

<块引用>

无效的运算符‘$in’"

因为 $filter 操作符不支持它.

现在我知道我可以使用 $unwind 并使用常规的 $match 操作符来做到这一点,但是如果需要使用 $group 将结果设置回数组 - 我不想要这个

我的问题是,是否有其他方法可以操作 $filter 运算符以获得我想要的结果.

解决方案

由于 $in 不支持数组的聚合操作,您可以使用 $setIsSubset.有关这方面的更多信息,您可以参考 this 链接.聚合查询现在看起来像

db.test.aggregate([{$项目:{'filtered_users':{$过滤器:{输入:'$用户',如:'用户',条件: {$setIsSubset: [['x'], '$$user.accounts']}}}}}])

此查询将仅返回将 [x] 作为 user.accounts 中数组子集的元素.

Right now, it's not possible to use the $in operator in the $filter array aggregation operator.

Let's say this is the document schema:

{
    _id: 1,
    users: [
        {
            _id: 'a',
            accounts: ['x', 'y', 'z']
        },
        {
            _id: 'b',
            accounts: ['j','k','l']
        }
    ]
}

I want, using aggregate, to get the documents with filtered array of users based on the contents of the accounts array.

IF the $in would work with the $filter operator, I would expect it to look like this:

db.test.aggregate([
    {
        $project: {
            'filtered_users': {
                $filter: {
                    input: '$users',
                    as: 'user',
                    cond: {
                        $in: ['$$user.accounts', ['x']]
                    }
                }
            }
        }
    }
])

and return in the filtered_users only the first user since x is in his account.

But, as I said, this doesn't work and I get the error:

"invalid operator '$in'"

because it isn't supported in the $filter operator.

Now I know I can do it with $unwind and using regular $match operators, but then it will be much longer (and uglier) aggregation with the need of using $group to set the results back as an array - I don't want this

My question is, if there is some other way to manipulate the $filter operator to get my desired results.

解决方案

Since $in is not supported in aggregate operation for array, the alternative would be for you to use $setIsSubset. For more information on this you can refer this link. The aggregate query would now look like

db.test.aggregate([
{
    $project: {
        'filtered_users': {
            $filter: {
                input: '$users',
                as: 'user',
                cond: {
                   $setIsSubset: [['x'], '$$user.accounts']           
                }
            }
        }
    }
}])

This query will return only elements which have [x] as a subset of the array in user.accounts.

这篇关于在 $project 阶段使用 $in 运算符过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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