我有一个与C#下面的对象相似的对象。我想找到匹配的人员对象,其中包含与提供的项目相匹配的项目,type
并且该项目的项目sourceId
也存在于提供的数组中sourceIds
:
Person: {
id: 1,
items: [
{
type: "one",
sourceId: 2
},
{
type: "two"
sourceId: 3
}
]
}
到目前为止,我已经提出了:
var filter = Builders<Person>.Filter.In(p => p.items.Where(i => i.type == "one").FirstOrDefault().sourceId, providedIds);
var results = PersonCollection.FindAsync(filter);
当我运行时,出现以下错误:
无法确定p => p.items.Where(i =>(i.type ==“ one”))的序列化信息。FirstOrDefault()。sourceId
从我可以找到的内容来看,似乎Mongo驱动程序无法理解FirstOrDefault
(或者可能在哪里)。我如何才能完成此查询?
MongoDB .NET驱动程序无法解释您的过滤器并将其转换为MongoDB查询语言。
您必须改用$ elemMatch,过滤器如下所示:
{ "items" : { "$elemMatch" : { "type" : "one", "sourceId" : { "$in" : [1, 2, 3] } } } }
C#版本:
var filter = Builders<Person>.Filter.ElemMatch(f => f.items,
item => item.type == "one" && providedIds.Contains(item.sourceId));
生成与$elemMatch
我上面粘贴的语句完全相同的语句
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句