How to use MongoRegex (MongoDB C# Driver)

后端 未结 3 2138
我在风中等你
我在风中等你 2020-12-11 23:42

Has anyone have any idea how to use MongoRegex for the document search?

I attempted this, but returns nothing back:

var spec = new Document();
spec.A         


        
相关标签:
3条回答
  • 2020-12-11 23:47

    After digging the source code, I finally found the answer :)

    var spec = new Document();
    spec.Add("Name", new MongoRegex(".*" + searchKey + ".*", "i"));
    collection.Find(spec)
    
    0 讨论(0)
  • 2020-12-11 23:50

    you just want a simple prefix query. Your regex is then ^ + searchKey. Also, this form will allow mongodb to use an index on Name.

    var spec = new Document("Name", new MongoRegex(string.Format("^{0}",searchKey), "i"));
    collection.Find(spec)
    
    0 讨论(0)
  • 2020-12-11 23:53

    I think you need to not include the "/"s in C#, i.e.,

    spec.Add("Name", new MongoRegex(searchKey + "*", "i"));
    
    0 讨论(0)
提交回复
热议问题