简体   繁体   English

MongoDb c#driver按字段值查找数组中的项目

[英]MongoDb c# driver find item in array by field value

i found the way to check is the value contains in simple array : 我发现检查的方法是在简单数组中包含的值:

var filter = Builders<Post>.Filter.AnyEq(x => x.Tags, "mongodb");

But how to find a complex item with many fields by a concrete field ? 但是如何通过具体领域找到包含许多字段的复杂项目? I found the way to write it via the dot notation approach with BsonDocument builder, but how can i do it with typed lambda notations ? 我找到了用BsonDocument构建器通过点符号方法编写它的方法,但是如何使用键入的lambda符号呢?

upd UPD

i think it some kind of 我觉得它有点像

builderInst.AnyIn(p => p.ComplexCollection.Select(ml => ml.Id), mlIds)

but can't check right now, is anyone could help ? 但现在无法检查,有人可以帮忙吗?

There is ElemMatch ElemMatch

var filter = Builders<Post>.Filter.ElemMatch(x => x.Tags, x => x.Name == "test");
var res = await collection.Find(filter).ToListAsync()

You need the $elemMatch operator. 你需要$elemMatch运算符。 You could use Builders<T>.Filter.ElemMatch or an Any expression: 您可以使用Builders<T>.Filter.ElemMatchAny表达式:

Find(x => x.Tags.Any(t => t.Name == "test")).ToListAsync()

http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/expressions/#elemmatch http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/expressions/#elemmatch

Here's an example that returns a single complex item from an array (using MongoDB.Driver v2.5.0): 这是一个从数组中返回单个复杂项的示例(使用MongoDB.Driver v2.5.0):

Simple Data Model 简单数据模型

public class Zoo
{
    public List<Animal> Animals { get; set; }
}

public class Animal
{
    public string Name { get; set; }
}

Option 1 (Aggregation) 选项1(聚合)

public Animal FindAnimalInZoo(string animalName)
{
    var zooWithAnimalFilter = Builders<Zoo>.Filter
        .ElemMatch(z => z.Animals, a => a.Name == animalName);

    return _db.GetCollection<Zoo>("zoos").Aggregate()
        .Match(zooWithAnimalFilter)
        .Project<Animal>(
            Builders<Zoo>.Projection.Expression<Animal>(z => 
                z.Animals.FirstOrDefault(a => a.Name == animalName)))
        .FirstOrDefault(); // or .ToList() to return multiple
}

Option 2 (Filter & Linq) This was about 5x slower for me 选项2(Filter&Linq)这对我来说慢了大约5倍

public Animal FindAnimalInZoo(string animalName)
{
    // Same as above
    var zooWithAnimalFilter = Builders<Zoo>.Filter
        .ElemMatch(z => z.Animals, a => a.Name == animalName);

    var zooWithAnimal = _db.GetCollection<Zoo>("zoos")
        .Find(zooWithAnimalFilter)
        .FirstOrDefault();

    return zooWithAnimal.Animals.FirstOrDefault(a => a.Name == animalName);
}

As of the 2.4.2 release of the C# drivers, the IFindFluent interface can be used for querying on array element. 从2.4.2版本的C#驱动程序开始,IFindFluent接口可用于查询数组元素。 ElemMatch cannot be used on an array of strings directly, whereas the find interface will work on either simple or complex types (eg 'Tags.Name') and is strongly typed. ElemMatch不能直接用于字符串数组,而find接口可以处理简单或复杂类型(例如“Tags.Name”)并且是强类型的。

            FilterDefinitionBuilder<Post> tcBuilder = Builders<Post>.Filter;
            FilterDefinition<Post> tcFilter = tcBuilder.Eq("Tags","mongodb") & tcBuilder.Eq("Tags","asp.net");
               ...
            await myCollection.FindAsync(tcFilter);

Linq driver uses the aggregation framework, but for a query with no aggregation operators a find is faster. Linq驱动程序使用聚合框架,但对于没有聚合运算符的查询,查找更快。

Note that this has been broken in previous versions of the driver so the answer was not available at the time of original posting. 请注意,在以前版本的驱动程序中已经破坏了这一点,因此在原始发布时无法获得答案。

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

相关问题 更新数组mongodb c#driver中的字段 - Update field in array mongodb c# driver MongoDb C#驱动程序-通过索引字段获取项目非常慢 - MongoDb C# Driver - Get item by indexed field very slow 如何使用 mongodb .net 驱动程序 C# 在集合中查找嵌套数组字段包含特定字符串的项目? - How to find items in a collection that nested array field contains specific string using mongodb .net driver C#? 如何使用 C# 驱动程序 2.10.4 查找 MongoDB 中特定字段的最小值 - How to find the min value of a specific field in MongoDB using C# driver 2.10.4 如何使用 Mongodb c# 驱动程序更新文档的数组字段的特定索引值? - How to update document's array field's specific indexed value using Mongodb c# driver? MongoDB 驱动程序 C# 更新嵌套对象数组中的单个字段 - MongoDB Driver C# Update a single field in Nested Array of Objects 如何在 C# 的 MongoDB 驱动程序中获取数组中一个字段的值 - How to get values of one field in array in MongoDB Driver for C# 使用C#驱动程序在mongodb中查找计算值最高的文档 - Find document with highest calculated value in mongodb using c# driver C#Mongodb。 在数组中查找项目并仅选择此项目 - C# Mongodb. Find item in array and select only this item MongoDB C# 驱动程序和日期时间字段 - MongoDB C# driver and DateTime field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM