Case Insensitive search with $in

后端 未结 6 1409
没有蜡笔的小新
没有蜡笔的小新 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:46

    Here is my case insensitive search (query) with multiple condition (regex) from data of array, I've used $in but it doesn't support case insensitive search.

    Example Data

    { 
      name : "...Event A",
      tags : ["tag1", "tag2", "tag3", "tag4] 
    },
    { 
      name : "...Event B",
      tags : ["tag3", "tag2"]
    },
    { 
      name : "...Event C",
      tags : ["tag1", "tag4"]
    },
    { 
      name : "...Event D",
      tags : ["tag2", "tag4"]
    }
    

    My query

    db.event.find(
      { $or: //use $and or $or depends on your needs
        [ 
          { tags : { 
             $elemMatch : { $regex : '^tag1$', $options : 'i' }
            } 
          },
          { tags : { 
             $elemMatch : { $regex : '^tag3$', $options : 'i' }
            } 
          }
        ]
    })
    

提交回复
热议问题