MongoDB C#2.4.4过滤文档,其中字符串数组的所有元素都在另一个数组中

时间:2017-09-11 08:55:08

标签: c# arrays mongodb filter aggregation-framework

我正在使用最新的C#MongoDB驱动程序(2.4.4)。我找不到一种方法来过滤一个集合,其中字符串数组属性的所有元素都在另一个数组中(作为输入参数)。

数据:[" a"," b"] - 输入:[" a"," c"," b"] =>行

数据:[" a"," b"] - 输入:[" a"," c"] => KO

这是我的模型的轻版本。 我有一个发货集合,每个发货都有一个字符串数组函数和subDocument数组的shippingDocuments。每个shippingDocument也有一个字符串数组函数。

shipment[]
   reference
   functions[]
   shippingDocument[]
           functions[]

我在输入参数(myFunctions)中有一个字符串数组。 基本上我想用带有聚合管道的C#API来实现这个mongo shell命令。

{$match:{"functions":{$in:myFunctions,$not: {"$elemMatch":{"$nin":myFunctions}}}}}

=>如果找到一个不在输入数组中的单个元素,它就是KO。

我需要对shippingDocument数组做同样的事情,但是对于$ project来说完全没问题。

var db = client.GetDatabase("ecom");
var collection = db.GetCollection<Shipment>("shipment");

var query = collection.Aggregate()
.Match(
    Builders<Shipment>.Filter.Eq(x => x.reference, "XXX") &
    (/*{$match:{"functions":{$in:myFunctions,$not: {"$elemMatch":{"$nin":myFunctions}}}}}*/) 
   )
.Project(s => 
    new
    {
        Reference = s.reference,
        Documents = s.shippingDocuments.Where(d => d.functions.All(ro => myFunctions.Contains(ro)))
    }
);

如果尝试添加匹配项,

Builders<Shipment>.Filter.Where(x => x.functions.All(f => myFunctions.Contains(f)))

我有例外,

Unsupported filter: All({document}{functions}.Where(Contains(value(System.Collections.Generic.List`1[System.String])))).

实现这一目标的任何想法?

0 个答案:

没有答案
相关问题