Mongodb C#驱动程序仅返回数组中匹配的子文档 [英] Mongodb C# driver return only matching sub documents in array
问题描述
我有一个这种格式的文件:
I have a document in this format:
{
_id: ...,
myArray: [{other: stuff}, {other: stuff}, ...],
...
}
我想找到与某些内容匹配的元素,例如_id
或myArray
子文档中的字段值.
I want to find elements that match certain things, like the _id
or fields value from the sub-documents in myArray
.
我想返回文档,但要过滤后的MyArray
,其中仅显示匹配的子文档.
I want to return the documents, but with a filtered MyArray
where only the matching sub-documents are present.
我试图做一个投影,并包含匹配的元素,如下所示:
I tried to do a projection and include the matched elements like this:
_mongoContext.myDocument
.Find(x => x.id == id & x.myArray.Any(y => myList.Contains(t.other)))
.Project<myModel>(Builders<myModel>.Projection.Include("myArray.$"))
我认为,这应该只返回与myArray
相匹配的第一个元素,而不是所有文档,而这不是我想要的(我希望与查询匹配的所有子文档都出现在返回的文档中)
This, I think, should only return the first element that matched in myArray
instead of all documents, which is not what I want (I want all sub-documents that match the query to be present in the returned document).
无论如何,它甚至都无法正常工作,我收到了positional projection does not match the query document
错误.也许是因为我没有使用FindOne
?
And anyway it did not even work, I'm getting a positional projection does not match the query document
error. Maybe it's because I'm not using FindOne
?
在任何情况下,我如何实现所需的内容? (请以粗体显示问题)
In any case, how can I achieve what I'm looking for? (See question in bold)
推荐答案
Typically you need to use $filter in Aggregation Framework to filter nested array. However there's an easier way to achieve that using MongoDB .NET Driver and IQueryable interface.
考虑最简单的模型:
public class MyModel
{
public string _id { get; set; }
public IEnumerable<MyNestedModel> myArray { get; set; }
}
public class MyNestedModel
{
public string other { get; set; }
}
及以下数据:
var m = new MyModel()
{
_id = "1",
myArray = new List<MyNestedModel>() {
new MyNestedModel() { other = "stuff" },
new MyNestedModel() { other = "stuff" },
new MyNestedModel() { other = "stuff2" } }
};
Col.InsertOne(m);
您可以简单地在集合上调用.AsQueryable()
,然后编写LINQ查询,该查询将由MongoDB驱动程序转换为$filter
,请尝试:
you can simply call .AsQueryable()
on your collection and then you can write LINQ query which will be translated by MongoDB driver to $filter
, try:
var query = from doc in Col.AsQueryable()
where doc._id == "1"
select new MyModel()
{
_id = doc._id,
myArray = doc.myArray.Where(x => x.other == "stuff")
};
var result = query.ToList();
或者,您可以将$filter
部分作为原始字符串编写,然后使用.Aggregate()
方法.使用这种方法,您不必映射"所有属性,但是缺点是您丢失了类型安全性,因为这只是一个字符串,请尝试:
Alternatively you can write $filter
part as a raw string and then use .Aggregate()
method. Using this approach you don't have to "map" all properties however the drawback is that you're losing type safety since this is just a string, try:
var addFields = BsonDocument.Parse("{ \"$addFields\": { myArray: { $filter: { input: \"$myArray\", as: \"m\", cond: { $eq: [ \"$$m.other\", \"stuff\" ] } } } } }");
var query = Col.Aggregate()
.Match(x => x._id == "1")
.AppendStage<MyModel>(addFields);
$addFields
用于覆盖现有字段.
这篇关于Mongodb C#驱动程序仅返回数组中匹配的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!