[英]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.ElemMatch
或Any
表达式:
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.