简体   繁体   English

使用MongoDB C#驱动程序2.2对嵌套字段进行强类型查询

[英]Strongly typed query on nested field using MongoDB C# driver 2.2

Consider the following structures 考虑以下结构

public class Parent
{
    public ObjectId Id { get; set; }    
    public IEnumerable<Child> Children { get; set; }
}

public class Child
{
    public string Value { get; set; }
}

I want to find all parents object whose children values are a superset of an array ie 我想找到所有父对象,其子值是数组的超集,即

var parents = new List<Parent>();
var values = new[] { "A", "B", "C" };
parents.Where(x => !values.Except(x.Children.Select(y => y.Value)).Any());

or 要么

{ "Children.Value": { $all: ["A", "B", "C"] } }

I'd like to do it in a typed manner but the predicate translator doesn't support Enumerable.Select so this won't work: 我想以键入的方式执行它,但谓词翻译器不支持Enumerable.Select所以这不起作用:

Builders<Parent>.Filter.All(x => x.Children.Select(y => y.Value), values);

I'm currently using this workaround: 我目前正在使用此解决方法:

var filters = values.Select(x => Builders<Parent>.Filter.Where(y => y.Children.Any(z => z.Value == x)));
Builders<Parent>.Filter.And(filters);

Is there a better way without using a magic field name string? 没有使用魔术字段名称字符串有更好的方法吗?

IMongoClient _client = new MongoClient(@"mongodb://...");
IMongoDatabase _database = _client.GetDatabase("...");
IMongoCollection<Parent> _collection = _database.GetCollection<Parent>("q35135879");
var ca = new Child { Value = "A" };
var cb = new Child { Value = "B" };
var cc = new Child { Value = "C" };
var fdb = Builders<Parent>.Filter;
var filterLinq = fdb.All (x=>x.Children, new[] {ca, cb, cc});
var filterFieldDefinition = fdb.All("Children", new[] { ca, cb, cc });
var found1 = _collection.Find(filterLinq).ToList();
var found2 = _collection.Find(filterFieldDefinition ).ToList();
CollectionAssert.AreEqual(found1, found2);

暂无
暂无

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

相关问题 MongoDB驱动程序C#强类型嵌套索引 - MongoDB Driver C# Strongly Typed Nested Index mongoDB,使用C#官方驱动程序的强类型集合 - mongoDB, strongly typed collection using C# oficial driver 如何使用 C# MongoDB 驱动程序编写强类型过滤器,其中嵌套的 object 属性不能相等? - How to do I write a strongly typed filter where nested object properties cannot be equal using C# MongoDB driver? 如何在C#Mongodb强类型驱动程序中删除嵌套文档数组内的元素 - How to delete an element inside array of a nested document in C# Mongodb strongly typed driver 如何在C#Mongodb强类型驱动程序中基于嵌套数组元素进行过滤 - How to filter based on nested array element in C# Mongodb strongly typed driver 如何在 C# Mongodb 强类型驱动程序中基于嵌套数组元素创建索引 - How to create index based on nested array element in C# Mongodb strongly typed driver 如何在 C# Mongodb 强类型驱动程序中存储和查询坐标数组 - How to store and query an array of coordinates in C# Mongodb strongly typed driver 使用C#Mongodb强类型驱动程序在事务中创建多个索引 - Create multiple indexes in a transaction using C# Mongodb strongly typed driver C# mongodb 使用记录的强类型 ID - C# mongodb with strongly typed ID using records 如何使用官方 MongoDB C# 驱动程序将 BsonDocument 转换为强类型对象? - How to convert a BsonDocument into a strongly typed object with the official MongoDB C# driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM