Case Insensitive search with $in

后端 未结 6 1417
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 06:44

    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"]
       }
    ]
    

提交回复
热议问题