MongoDB C#驱动程序,使用正则表达式按数组元素查询 [英] MongoDB C# driver, query by an array element using regex
本文介绍了MongoDB C#驱动程序,使用正则表达式按数组元素查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MongoDB C#驱动程序 2.2版.我的收藏包含父母"对象.每个父对象都有一个子对象数组.每个孩子都有名字值:
I am using MongoDB C# driver version 2.2. My collection contains "Parent" objects. Each parent object has an array of children objects. Each child has name value:
"parent": {
"children":[
{ "name": "Bob", "age": 10},
{ "name": "Alice", "age": 7},
{ "name": "Tobias", "age": 11}
]
}
我需要将以下代码转换为C#语句/LINQ语法:
I need to translate the following code into C# statements / LINQ syntax:
db.getCollection('Parents').find({'parent.children': { $elemMatch: { 'name': { $regex: '.*ob.*', $options: 'im' } }}})
我发现有类似的方法
var builder = Builders<BsonDocument>.Filter;
builder.Regex("parent.children.name", new BsonRegularExpression(".*ob.*")); //does not work with array
和
builder.AnyEq("parent.children.name", "ob"); //without regex
但是我不明白如何将它们结合起来.请告知.
But I cannot understand how to combine them. Please advise.
更新:
我现在正在使用以下内容,如果您知道它不能正常运行的原因,请纠正我:
I am using the following for now, please correct me if you know a reason why it should not work correctly:
builder.AnyEq("parent.children.name", new BsonRegularExpression(".*ob.*"))
推荐答案
我现在正在使用以下内容:
I am using the following for now:
builder.AnyEq("parent.children.name", new BsonRegularExpression(".*ob.*"))
这篇关于MongoDB C#驱动程序,使用正则表达式按数组元素查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文