例子:

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

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

当前回答

你可以使用正则表达式。

在你的例子中,这将是:

db.stuff.find( { foo: /^bar$/i } );

不过,我必须说,也许你可以降低(或提高)价值的过程中,而不是承担额外的成本,每次你找到它。显然,这对人名之类的东西不起作用,但可能用在像标签这样的用例上。

其他回答

我很惊讶没有人警告通过使用/^bar$/ I正则表达式注入的风险,如果bar是密码或帐户id搜索。例如,bar => .*@myhackeddomain.com,所以我的打赌是:使用\Q \E正则表达式特殊字符!PERL提供

db.stuff.find( { foo: /^\Qbar\E$/i } );

当bar = '\E *@myhackeddomain.com\Q'时,您应该使用\\字符转义bar变量,以避免再次被\E利用

另一种选择是使用一个regex转义字符策略,就像这里描述的Javascript等价于Perl的\Q…\E或quotemeta()

db.zipcodes.find({city : "NEW YORK"}); // Case-sensitive
db.zipcodes.find({city : /NEW york/i}); // Note the 'i' flag for case-insensitivity

从MongoDB 3.4开始,执行快速不区分大小写搜索的推荐方法是使用不区分大小写索引。

我亲自给其中一位创始人发了邮件,请他把这个工作做好,他做到了!自2009年以来,JIRA上就有这个问题,许多人都要求提供这个功能。下面是它的工作原理:

通过指定强度为1或2的排序规则,可以创建不区分大小写的索引。你可以像这样创建一个不区分大小写的索引:

db.cities.createIndex(
  { city: 1 },
  { 
    collation: {
      locale: 'en',
      strength: 2
    }
  }
);

你也可以在创建集合时指定一个默认的排序规则:

db.createCollection('cities', { collation: { locale: 'en', strength: 2 } } );

在这两种情况下,为了使用不区分大小写的索引,你需要在find操作中指定与创建索引或集合时使用的相同的排序规则:

db.cities.find(
  { city: 'new york' }
).collation(
  { locale: 'en', strength: 2 }
);

这将返回"New York", "New York", "New York"等。

其他的笔记

The answers suggesting to use full-text search are wrong in this case (and potentially dangerous). The question was about making a case-insensitive query, e.g. username: 'bill' matching BILL or Bill, not a full-text search query, which would also match stemmed words of bill, such as Bills, billed etc. The answers suggesting to use regular expressions are slow, because even with indexes, the documentation states: "Case insensitive regular expression queries generally cannot use indexes effectively. The $regex implementation is not collation-aware and is unable to utilize case-insensitive indexes." $regex answers also run the risk of user input injection.

如果你正在使用MongoDB Compass:

转到筛选器类型的集合-> {Fieldname: /string/i}

对于使用Mongoose的Node.js:

模型。find({FieldName: {$regex: "stringToSearch", $options: "i"}})

对于任何使用Golang并希望使用mongodb和mgo godoc globalsign库进行区分大小写的全文搜索的人。

collation := &mgo.Collation{
    Locale:   "en",
    Strength: 2, 
}


err := collection.Find(query).Collation(collation)