$ nin与$ expr [英] $nin with the $expr

查看:112
本文介绍了$ nin与$ expr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询来查找用户CreatedBy是否在SharedWith中.我想反查询,以检查CreatedBy是否不在SharedWith中.

I have a query to find if a user CreatedBy is in a SharedWith. I want to inverse the query to check if the CreatedBy is not in SharedWith.

[
    {
        "$match": {
            "$and": [
                {
                    "$and": [
                        {
                            "SharedWith": {
                                "$exists": true
                            }
                        },
                        {
                            "$expr": {
                                "$in": [
                                    "$CreatedBy",
                                    "$Multi_User"
                                ]
                            }
                        }
                    ]
                }
            ]
        }
    }
]

MongoDB不支持直接对$and进行查询的$nin$not.

MongoDB does not support a direct $nin or $not for $and query.

任何想法如何实现这一目标.

Any idea how to achieve this.

用户文档如下所示,

   Collection = [
        {"CreatedBy":
             {"_id": "User001",
              "Email": "user@eg.com",
              "Name": "User001"},
         "SharedWith": [
             {"_id": "User001",
              "Email": "user@eg.com",
              "Name": "User001"},
             {"_id": "User002",
              "Email": "user@eg.com",
              "Name": "User002"},
             {"_id": "User003",
              "Email": "user@eg.com",
              "Name": "User003"},
         ]}

    ]

推荐答案

$nin 是查询运算符,不是聚合运算符,也是 $expr 仅支持aggregation运算符,而不支持query运算符.因此,您可能应该使用 $not $in 使用 $expr 表达式 以这种方式

$nin is an query operator not an aggregation operator and also $expr only supports the aggregation operators not the query ones. So, You should probably use $not $in with the $expr expression in this manner

{
  "$match": {
    "$and": [
      {
        "$or": [
          {
            "Multi_User": {
              "$exists": False
            }
          },
          {
            "$expr": {
              "$not": { "$in": ["$CreatedBy", "$Multi_User"] }
            }
          }
        ]
      }
    ]
  }
}

这篇关于$ nin与$ expr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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