C#MongoDB驱动程序:找不到在MongoDB中为AnyIn筛选器运行复杂查询的方法 [英] C# MongoDB Driver: Can't find the way to run complex query for AnyIn filter in MongoDB

查看:57
本文介绍了C#MongoDB驱动程序:找不到在MongoDB中为AnyIn筛选器运行复杂查询的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的文件:

{
  "id": "xxxxxxxxxxxx",
  "groupsAuthorized": [
    "USA/California/SF",
    "France/IDF/Paris"
  ]
}

我有一个用户,其中包含授权组的列表,例如以下内容:

And I have an user that has a list of authorized groups, like for example the following:

"groups": [
  "France/IDF",
  "USA/NY/NYC"
]

我要实现的目标是检索用户有权检索的数据库中的所有文档,本质上讲,我希望能够在列表"groupsAuthorized"中签入(如果该组中的一个包含以下内容的子集)我的用户授权中包含的其他列表组"的元素

What I'm trying to achieve is to retrieve all documents in the database that the user is authorized to retrieve, essentially I want to be able to check in the list "groupsAuthorized" if one of the group contains a subset of an element of the other list "groups" contained in my user authorizations

使用以下值:

my document:
{
  "id": "xxxxxxxxxxxx",
  "groupsAuthorized": [
    "USA/California/SF",
    "France/IDF/Paris"
  ]
}

my user permissions:
"groups": [
  "France/IDF",
  "USA/NY/NYC"
]

由于字符串"France/IDF"正确包含在字符串"France/IDF/Paris"中,因此用户应该能够检索此文档,但是,如果值是这样的话:

the user should be able to retrieve this document as the string "France/IDF" is correctly contained in the string "France/IDF/Paris", however, if the values would've been like this:

my document:
{
  "id": "xxxxxxxxxxxx",
  "groupsAuthorized": [
    "USA/California/SF",
    "France/IDF"
  ]
}

my user permissions:
"groups": [
  "France/IDF/Paris",
  "USA/NY/NYC"
]

它不起作用,因为我的用户仅被授权查看来自France/IDF/Paris和USA/NY/NYC的文档,而我的AuthorizedGroups内的字符串均不包含这些序列

it should not work, because my user is only authorized to view documents from France/IDF/Paris and USA/NY/NYC and none of the string inside of the authorizedGroups of my document contains those sequences

我试图使用标准的LINQ查询来实现这一点,这很简单:

I've tried to use a standard LINQ query to achieve this which is fairly simple:

var userAuthorizedGroups = new List<string> { "France/IDF/Paris", "USA/NY/NYC" };
var results = collection.AsQueryable()
.Where(entity => userAuthorizedGroups
                .Any(userGroup => entity.authorizedGroups
                                  .Any(entityAuthorizedGroup => entityAuthorizedGroup.Contains(userGroup))));

但是我得到了似乎很多人都无法理解的著名的不受支持的过滤器异常,我已经尝试过在互联网上找到如下所示的其他选项:

But i'm getting the famous unsupported filter exception that it seems lot of people is having, i've tried different options found on the internet like the following:

var userAuthorizedGroups = new List<string> { "France/IDF/Paris", "USA/NY/NYC" };
var filter = Builders<PartitionedEntity<Passport>>.Filter.AnyIn(i => i.authorizedGroups, userAuthorizedGroups);
var results = (await collection.FindAsync(filter)).ToList();

return results;

但是问题是,这只会检查该数组的一个元素是否包含在另一个数组中,对于"France/IDF"之类的情况应该正确匹配"France/IDF/Paris",它将无法正常工作因为我的文档中的"France/IDF/Paris"字符串中包含"France/IDF"字符串

But the problem is this will only check if one of the element of the array is contained inside the other array, It will not correctly work for case like "France/IDF" that should correctly match "France/IDF/Paris" because "France/IDF" string is contained inside the "France/IDF/Paris" string inside of my document

我对如何使用mongodb C#驱动程序实现这一目标一无所知,我开始认为我应该将所有文档拉到客户端并手动进行过滤,但这会很麻烦

I'm getting a bit clueless on how to achieve this using the mongodb C# driver, i'm starting to think that I should just pull all documents to client and do the filtering manually but that would be quite messy

有人对此主题有想法吗?

Has anyone an Idea on this subject ?

推荐答案

我开始认为我应该将所有文档拉到客户端并手动进行过滤,但这会很麻烦

i'm starting to think that I should just pull all documents to client and do the filtering manually but that would be quite messy

不要这样做:)

可以开始的一个地方是这里.它描述了MongoDB .NET驱动程序支持的所有LINQ运算符.如您所见,这里没有提到 .Contains(),这意味着您无法使用它,并且在运行时会收到一个错误,但这并不意味着没有办法做您正在努力实现.

One place you can start with is here. It describes all the LINQ operators that are supported by the MongoDB .NET driver. As you can see .Contains() isn't mentioned there which means you can't use it and you'll get an arror in the runtime but it does not mean that there's no way to do what you're trying to achieve.

您可以使用的最接近包含的运算符是 $ indexOfBytes 如果没有匹配项,则返回 -1 ,否则返回子字符串的位置.另外,由于您需要将一个数组与另一个数组进行匹配,因此需要两对 $ anyElementTrue 完全执行.NET的 .Any 的工作.

The operator closest to contains you can use is $indexOfBytes which returns -1 if there's no match and the position of a substring otherwise. Also since you need to match an array against another array you need two pairs of $map and $anyElementTrue to do exactly what .NET's .Any does.

您的查询(MongoDB客户端)如下所示:

Your query (MongoDB client) can look like this:

db.collection.find({
    $expr: {
        $anyElementTrue: {
            $map: {
                input: "$groupsAuthorized",
                as: "group",
                in: {
                    $anyElementTrue: {
                        $map: { 
                            input: ["France/IDF/Paris", "USA/NY/NYC"],
                            as: "userGroup",
                            in: { $ne: [ -1, { $indexOfBytes: [ "$$userGroup", "$$group" ] } ] }
                        }
                    }
                }
            }
        }
    }
})

蒙戈游乐场

您可以使用 BsonDocument 类从.NET运行相同的查询,该类接受字符串(JSON)并转换为查询:

You can run the same query from .NET using BsonDocument class which takes a string (JSON) and converts into a query:

var query = BsonDocument.Parse(@"{
    $expr: {
        $anyElementTrue:
        {
            $map:
            {
                input: '$groupsAuthorized',
                    as: 'group',
                    in: {
                    $anyElementTrue:
                    {
                        $map:
                        {
                            input: ['France/IDF/Paris', 'USA/NY/NYC'],
                                as: 'userGroup',
                                in: { $ne: [-1, { $indexOfBytes: ['$$userGroup', '$$group'] } ] }
                        }
                    }
                }
            }
        }
    }
}");

var result = col.Find(query).ToList();

这篇关于C#MongoDB驱动程序:找不到在MongoDB中为AnyIn筛选器运行复杂查询的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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