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

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

问题描述

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

<预><代码>[{$匹配":{"$and": [{"$and": [{跟...一起分享": {"$exists": 真}},{$expr":{"$in": ["$CreatedBy",$Multi_User"]}}]}]}}]

MongoDB 不支持用于 $and 查询的直接 $nin$not.

知道如何实现这一点.

用户文档看起来像这样,

 集合 = [{由...制作":{"_id": "User001","电子邮件": "user@eg.com","Name": "User001"},跟...一起分享": [{"_id": "User001","电子邮件": "user@eg.com","Name": "User001"},{"_id": "User002","电子邮件": "user@eg.com","Name": "User002"},{"_id": "User003","电子邮件": "user@eg.com","Name": "User003"},]}]

解决方案

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

<代码>{$匹配":{"$and": [{$或":[{多用户": {$exists":错误}},{$expr":{"$not": { "$in": ["$CreatedBy", "$Multi_User"] }}}]}]}}

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 does not support a direct $nin or $not for $and query.

Any idea how to achieve this.

The user document looks like 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 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天全站免登陆