MongoDB Query with a Tuple

本文关键字:Tuple with Query MongoDB | 更新日期: 2023-09-27 18:33:46

>我有以下文档结构:

{
  Foo: BinData
  Bar: Integer
}

我想通过两个字段Foo和Bar搜索这些文档(它们还有其他字段)(它们共同构成了要搜索的键)。

我目前正在这样做:

List<Tuple<byte[], int>> fooBarList = mySearchValues;
return Collection.AsQueryable().OfType<MyType>()
                .Where(x => fooBarList.Any(y => x.Foo == y.Item1 && x.Bar == y.Item2))
                .ToArray();

但是,Mongo不了解如何序列化此查询。

System.NotSupportedException: Unable to determine the serialization information for the expression: System.Collections.Generic.List`1[System.Tuple`2[System.Byte[],System.Int32]].
   at MongoDB.Driver.Linq.Utils.BsonSerializationInfoFinder.GetSerializationInfo(Expression node, Dictionary`2 serializationInfoCache)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildAnyQuery(MethodCallExpression methodCallExpression)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildMethodCallQuery(MethodCallExpression methodCallExpression)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
   at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
   at MongoDB.Driver.Linq.SelectQuery.Execute()
   at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

是否有人已经对此进行了类似的查询,如果是这样,我该怎么做?我不能使用$in,因为这只对单个文档字段进行操作。

MongoDB Query with a Tuple

驱动程序的当前版本 (1.x) 是为 .NET 3.5 构建的,并且本身不知道如何序列化元组。如果需要,可以编写元组序列化程序并注册它,使其可以工作。

但是,我想不使用元组,而是创建一个简单的私有类并使用它会更容易。