Case Insensitive search with $in

后端 未结 6 1418
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 05:46

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 thos

6条回答
  •  甜味超标
    2020-12-15 06:35

    This works for me perfectly.

    From code we can create custom query like this:

    {
      "first_name":{
        "$in":[
          {"$regex":"^serina$","$options":"i"}, 
          {"$regex":"^andreW$","$options":"i"}
        ]
      }
    }
    

    This will transform to following in mongo after query:

    db.mycollection.find({"first_name":{"$in":[/^serina$/i, /^andreW$/i]}})
    

    Same for "$nin".

提交回复
热议问题