使用 $in 进行不区分大小写的搜索 [英] Case Insensitive search with $in

查看:28
本文介绍了使用 $in 进行不区分大小写的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 $in 搜索 mongodb 集合中的列,其中包括要搜索的元素数组以及列中这些元素的 caseInsensitive 匹配?

How to search a column in a collection in mongodb with $in which includes an array of elements for search and also caseInsensitive matching of those elements in the column ?

推荐答案

你可以使用$elemMatch 使用正则表达式搜索,例如让我们在以下集合中搜索blue"颜色:

You can use $elemMatch with regular expressions search, e.g. let's search for "blue" color in the following collection:

db.items.save({ 
  name : 'a toy', 
  colors : ['red', 'BLUE'] 
})

> ok

db.items.find({
  'colors': {
    $elemMatch: { 
      $regex: 'blue', 
      $options: 'i' 
    }
  }
})

>[ 
  {   
    "name": "someitem",
    "_id": { "$oid": "4fbb7809cc93742e0d073aef"},   
    "colors": ["red", "BLUE"]
   }
]

这篇关于使用 $in 进行不区分大小写的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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