Case Insensitive search with $in

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

    Use $in with the match being case insensitive:

    Data example:

    { 
        name : "...Event A",
        fieldX : "aAa" 
    },
    { 
      name : "...Event B",
      fieldX : "Bab" 
    },
    { 
      name : "...Event C",
      fieldX : "ccC" 
    },
    { 
      name : "...Event D",
      fieldX : "dDd" 
    }
    

    And we want documents were "fieldX" is contained in any value of the array (optValues):

    var optValues = ['aaa', 'bbb', 'ccc', 'ffffd'];
    
    var optRegexp = [];
    optValues.forEach(function(opt){
            optRegexp.push(  new RegExp(opt, "i") );
    });
    
    db.collection.find( { fieldX: { $in: optRegexp } } );
    

    This works for $all either.

    I hope this helps!

    p.s.: This was my solution to search by tags in a web application.

    0 讨论(0)
  • 2020-12-15 06:34

    The way to do it in Java is:

    List<String> nameList = Arrays.asList(name.split(PATTERN));
    List<Pattern> regexList = new ArrayList<>();
    for(String name: nameList) {
    regexList.add(Pattern.compile(name , Pattern.CASE_INSENSITIVE));
    }
    criteria.where("Reference_Path").in(regexList);
    
    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-12-15 06:43

    This is pretty simple

    const sampleData = [
      RegExp("^" + 'girl' + "$", 'i'),
      RegExp("^" + 'boy' + "$", 'i')
    ];
    const filerObj = { gender : {$in : sampleData}};
    
    0 讨论(0)
  • 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"]
       }
    ]
    
    0 讨论(0)
  • 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' }
            } 
          }
        ]
    })
    
    0 讨论(0)
提交回复
热议问题