Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MongoDB C#驱动程序write ElementMatch和Regex查询_C#_Mongodb_Mongo Shell - Fatal编程技术网

使用MongoDB C#驱动程序write ElementMatch和Regex查询

使用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

我需要使用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 }).sort({ "CreatedDateTimeUtc" : -1 })
如果您注意到它是在
/batch/s
处追加s,而不是在i
/batch/i

我怎样才能得到这份工作?我需要为过滤器这样做

  • 包含,使用.contains()
  • equals,正在考虑使用.equals()
  • 不包含,正在考虑使用!字段。包含(值)
  • 不等于
  • 我可以这样做吗,这样我就可以将我所有的正则表达式模式应用于上述所有过滤器

    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
    is
    FilterDefinition

    我的示例笔记集如下所示:

    {  
       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,
    
          }
       ]
    }