MongoDB: Is it possible to make a case-insensitive query?

后端 未结 24 2133
谎友^
谎友^ 2020-11-22 04:44

Example:

> db.stuff.save({\"foo\":\"bar\"});

> db.stuff.find({\"foo\":\"bar\"}).count();
1
> db.stuff.find({\"foo\":\"BAR\"}).count();
0

24条回答
  •  日久生厌
    2020-11-22 05:26

    I've created a simple Func for the case insensitive regex, which I use in my filter.

    private Func CaseInsensitiveCompare = (field) => 
                BsonRegularExpression.Create(new Regex(field, RegexOptions.IgnoreCase));
    

    Then you simply filter on a field as follows.

    db.stuff.find({"foo": CaseInsensitiveCompare("bar")}).count();
    

提交回复
热议问题