MongoDB query $in with regex array of element

前端 未结 5 1456
野趣味
野趣味 2020-12-16 18:09

Ok i am trying to implement a query, which is trying to perform regex search ( which contains an array list ) on a bunch of document

Its hard for me to explain...so

5条回答
  •  一个人的身影
    2020-12-16 18:52

    it does not work when the double quotes are present because they are interpreted as strings instead of as RegExp objects. So to make it to work, you have to convert it to RegExp objects first in Javascript like this.

    var sea = [ "xd", "sd", "ad" ]; // Note: no slashes
    var regex = [];
    for (var i = 0; i < sea.length; i++) {
        regex[i] = new RegExp(sea[i]);
    }
    db.paper.find({"category": {$in: regex}});
    

    Remember, MongoDB shell uses Javascript

提交回复
热议问题