[英]MongoDB push to nested array using Linq expressions
To execute a push using de MongoDB C# Driver, I need to instantiate a FieldDefinition<MyMongoDocumentType, MyNestedArrayType[]>
.要使用 de MongoDB C# Driver 执行推送,我需要实例化FieldDefinition<MyMongoDocumentType, MyNestedArrayType[]>
。
I know that I can instantiate this FieldDefinition
using strings...我知道我可以使用字符串实例化这个FieldDefinition
......
FieldDefinition<MyMongoDocumentType, NestedArrType[]> field = "MyArray.$.MyNestedArray";
I tried the same using Linq expressions, like this:我使用 Linq 表达式尝试了相同的操作,如下所示:
FieldDefinition<MyMongoDocumentType, NestedArrType[]> field =
new ExpressionFieldDefinition<MyMongoDocumentType, NestedArrType[]>(
doc => doc.MyArray.First().MyNestedArray
);
But I got this error:但我收到了这个错误:
System.InvalidOperationException: Unable to determine the serialization information for doc => doc.MyArray.First().MyNestedArray. System.InvalidOperationException:无法确定 doc => doc.MyArray.First().MyNestedArray 的序列化信息。
Is there any way to create a FieldDefinition
of a nested array using Linq expression that works?有没有办法使用有效的 Linq 表达式创建嵌套数组的FieldDefinition
?
You can use -1
as an array index to represent positional operator ( $
):您可以使用-1
作为数组索引来表示位置运算符 ( $
):
FieldDefinition<MyMongoDocumentType, NestedArrType[]> field =
new ExpressionFieldDefinition<MyMongoDocumentType, NestedArrType[]>(
doc => doc.MyArray[-1].MyNestedArray
);
To make it work you also need additional query condition on MyArray
which can be done using ElemMatch
in MongoDB .NET driver, for instance:为了使其工作,您还需要在MyArray
上附加查询条件,这可以在 MongoDB .NET 驱动程序中使用ElemMatch
完成,例如:
Builders<MyMongoDocumentType>.Filter.ElemMatch(x => x.MyArray, f => f.NestedId == 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.