我正在使用MongoDB C#驅動程序版本2.2。我的收藏包含「父」對象。每個父對象都有一個子對象數組。每個孩子都有名稱值:MongoDB C#驅動程序,使用正則表達式查詢數組元素
"parent": {
"children":[
{ "name": "Bob", "age": 10},
{ "name": "Alice", "age": 7},
{ "name": "Tobias", "age": 11}
]
}
我需要翻譯下面的代碼到C#語句/ LINQ語法:
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
B我不明白如何結合他們。請指教。
UPDATE:
我使用的,現在下面,請糾正我,如果你知道一個原因,它不應該正常工作:
builder.AnyEq("parent.children.name", new BsonRegularExpression(".*ob.*"))
您的最終查詢看起來對我來說很好,順便說一句! –