简体   繁体   English

如何使用 & 和 | 为 c# 中的 MongoDB 构建动态查询运营商

[英]How to build dynamic queries for MongoDB in c# using both & and | operators

So i have a list of key value pairs witch the key is a string and the value is an array of strings as follows:所以我有一个键值对列表,键是一个字符串,值是一个字符串数组,如下所示:

{
    "filters": [
        {"key": "team", "value": ["3","4","7"]},
        {"key": "placement_type", "value": ["facility"]}
        //{"key": "date", "value": ["2021-01-01"]}
        //{"key": "policy", "value": ["something"]} Number of filters may vary
    ]
}

This list of "filters" can have multiple Key Value pairs depending on the number of filters the user has chosen.这个“过滤器”列表可以有多个键值对,具体取决于用户选择的过滤器数量。

So what i need is to dynamically create is the following query:所以我需要动态创建的是以下查询:

(builder.Eq("team", "3") | builder.Eq("team", "4") | builder.Eq("team", "7")) & builder.Eq("placement_type", "Facility");

And this is my attempt:这是我的尝试:

var builder = Builders<ReportDocument>.Filter;

// IF I USE THIS QUERY IT WORKS AS I EXPECT
var hardCodedQuery = (builder.Eq("team", "3") | builder.Eq("team", "4") | builder.Eq("team", "7")) & builder.Eq("placement_type", "Facility");

var dynamicQuery = builder.Empty;
var orFilters = builder.Empty;

foreach (var filter in request.Filters)
{
  if (filter.Value.Length > 1)
  {
     for (int i = 0; i < filter.Value.Length; i++)
     {
        if (i == 0)
        {
          orFilters = builder.Eq(filter.Key, filter.Value[i]); //Had to do this since i have to initiate the variable as builder.Empty and it was adding and EmptyFilterDefinition
        }
        else
        {
          orFilters &= builder.Eq(filter.Key, filter.Value[i]);
        }
      }
      dynamicQuery = builder.Or(orFilters);
   }
   else
   {
      dynamicQuery &= builder.Eq(filter.Key, filter.Value);
   }
}
return await _reportsCollection.Find(dynamicQuery).ToListAsync();

But once i insert the second filter in the variable "orFilters" with the operator "&=" e becomes a MongoDB.Driver.但是一旦我在变量“orFilters”中插入第二个过滤器,运算符“&= e 就变成了 MongoDB.Driver。 AndFilterDefinition instead of MongoDB.Driver. AndFilterDefinition而不是 MongoDB.Driver。 OrFilterDefinition或过滤器定义

So if anyone has a good way to do this please share it guys.因此,如果有人有这样做的好方法,请分享给大家。 I saw a lot of questions similar like mine in stackoverflow but all refer do doing dynamic filters using just the & (And) operator.我在 stackoverflow 中看到了很多与我的问题类似的问题,但所有参考都只使用 & (And) 运算符做动态过滤器。 But since i have a list of strings as value i need to use the |但是因为我有一个字符串列表作为值,所以我需要使用 | (Or) operator as well. (或)运算符也是如此。

Thanks谢谢

Your first compound assignment doesn't match your expected query, in your example you use OR but in your code you are using AND assignment您的第一个复合分配与您的预期查询不匹配,在您的示例中您使用OR但在您的代码中您使用的是AND分配

....
if (filter.Value.Length > 1)
  {
     for (int i = 0; i < filter.Value.Length; i++)
     {
        if (i == 0)
        {
          orFilters = builder.Eq(filter.Key, filter.Value[i]); //Had to do this since i have to initiate the variable as builder.Empty and it was adding and EmptyFilterDefinition
        }
        else
        {
          // this line was changed 
          orFilters |= builder.Eq(filter.Key, filter.Value[i]);
        }
      }
      //also when this line run, you are resetting your filter so pay attention to it, should change
      dynamicQuery = builder.Or(orFilters);
   }
   .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM