使用MongoDB C#驱动程序write ElementMatch和Regex查询
我需要使用MongoDB C#驱动程序构造以下查询 我使用了这样的查询使用MongoDB C#驱动程序write ElementMatch和Regex查询,c#,mongodb,mongo-shell,C#,Mongodb,Mongo Shell,我需要使用MongoDB C#驱动程序构造以下查询 我使用了这样的查询 builder.ElemMatch(x => x.CustomFields, x => x.Value.Contains(filterValue)) 它生成了mongo查询,如下所示: db.Notes.find({ "Group._id" : 74, "CustomFields" : { "$elemMatch" : { "Value" : /batch/s } }, "IsDeleted" : false
builder.ElemMatch(x => x.CustomFields, x => x.Value.Contains(filterValue))
它生成了mongo查询,如下所示:
db.Notes.find({ "Group._id" : 74, "CustomFields" : { "$elemMatch" : { "Value" : /batch/s } }, "IsDeleted" : false }).sort({ "CreatedDateTimeUtc" : -1 })
如果您注意到它是在/batch/s
处追加s,而不是在i/batch/i
我怎样才能得到这份工作?我需要为过滤器这样做
builder.Regex(x => x.CustomFields[-1].Value, new BsonRegularExpression($"/{filterValue}/i"));
这会将查询转换为如下所示,但不会得到任何结果
db.Notes.find({ "Project._id" : 74, "CustomFields.$.Value" : /bat/i, "IsDeleted" : false }).sort({ "CreatedDateTimeUtc" : -1 })
仅供参考:builder
isFilterDefinition
我的示例笔记集如下所示:
{
Name:"",
Email:"",
Tel:"",
Date:02 /21/1945,
CustomFields:[
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
}
]
}
听起来你所缺少的只是不敏感的部分。你试过这个吗 ToLower,ToLowerInvariant,ToUpper,ToUpper不变量(字符串方法) 这些方法用于测试 文档以不区分大小写的方式匹配值 根据1.1文档,它说这将允许执行不区分大小写的正则表达式匹配。
当前的文档没有提到它,所以为了确保这一点,我检查了github,创建不敏感匹配的代码仍然是。不敏感部分是其中之一,我还需要在文本以开头、文本以结尾和文本不包含的位置编写查询。我想我只能用BsonRegularexpression做这样的事情?对不起,我错过了这个问题的一部分。mongo驱动程序将正确处理
String.StartsWith()
和String.EndsWith()
,将它们分别转换为/^
和$/
。它还应该变成一元非
进入mongo$not:{}
如何进行“不包含”搜索?
{
Name:"",
Email:"",
Tel:"",
Date:02 /21/1945,
CustomFields:[
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
},
{
Name:"",
Value:"",
IsSearchable:true,
}
]
}