mongodb上不区分大小写的查询 [英] case-insensitive query on mongodb

查看:94
本文介绍了mongodb上不区分大小写的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用map/reduce的情况下在mongo上查询不区分大小写的值?

is there a way to query for a case-insensitive value on mongo without using map/reduce?

推荐答案

假设您有包含tag字段的文档,并且要在其上进行搜索

Suppose you have document that contains tag field and you want search on it

Tags
{
  tag,
  ...
 }

第一个选择是使用正则表达式(但工作得很慢,如@RestRisiko所说):

First option is use regex(but it work slow as @RestRisiko said):

db.tags.find( { "tag" : { "$regex" : "C#", "$options" : "-i" } })

第二个选项是创建另一个小写字段(在mongodb中是最佳方式):

Second option is create another, lower case field( and in mongodb it best way):

Tags
{
  tag,
  tagLower,
  ..
}

并像往常一样使用find:

db.tags.find( { "tagLower" : "c#"})

它将更快地工作,因为上面的代码可以使用索引进行搜索.

It will work faster, because above code can use index for search.

这篇关于mongodb上不区分大小写的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆