19

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 I am basically coming to direct point.

There is query which works with regex array...

db.paper.find({"category": {$in: [ /xd/, /sd/, /ad/ ] }})

There is query which doesn't works with regex array...

db.paper.find({"category": {$in: [ "/xd/", "/sd/", "/ad/" ] }})

So basically what I want is remove "" sign from string array...so that i can perform below query..

var sea = [ "/xd/", "/sd/", "/ad/" ];
db.paper.find({"category": {$in: sea }});

6 Answers 6

31

Using $in can be fairly efficient with small arrays but not so well with huge lists since it will skip around in the index to find the matching documents, or walk through the whole collection if there isn't an index to use.

Besides using the $in with the regular expression, you could use a pipe-delimited regex pattern with the keywords list like this:

Test documents:

db.papertest.insert([
    { category: "ad bd cd" },
    { category: "dd ed fd" },
    { category: "gd hd id" },
    { category: "jd kd ld" },
    { category: "md nd od" },
    { category: "pd qd rd" },
    { category: "sd td ud" },
    { category: "vd wd xd yd zd" },
]);

The magic:

var keywords = ["xd", "sd", "ad"],
    regex = keywords.join("|");

db.papertest.find({
    "category": {
        "$regex": regex, 
        "$options": "i"
    } 
});

The results

{ "_id" : ObjectId("56bb6f171bb4f693057c0ba4"), "category" : "ad bd cd" }
{ "_id" : ObjectId("56bb6f171bb4f693057c0baa"), "category" : "sd td ud" }
{ "_id" : ObjectId("56bb6f171bb4f693057c0bab"), "category" : "vd wd xd yd zd" }
Sign up to request clarification or add additional context in comments.

2 Comments

nice, this should be the accepted answer
What a brilliant solution
13

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

1 Comment

how it can be achieved using MongoTemplate in spring boot application
2

It seems to be working fine for me please try this

var sea = [ "xd", "sd", "ad" ];

var regex = sea.map( function( val ){ 
        return new RegExp( '^['+val+'].*','i' ); 
    }) 

db.paper.find({"category": { $in: regex }});

Comments

1

For this you can add a regular expression to each item in the array, you can do it in the following way.

data = ['hoLA','Que','TAL', 'Nueva'];
data  = data.map(function(v, i){return new RegExp(v, 'i')});
MyCollection.find({"thing": {$in : data}}, function(err, data){
    if (err) {
        console.log(err)
    }else{
        data.forEach(function(item){
            console.log(item.nombre);
        })
    }
});

Comments

1

Slightly improved ES6 + TypeScript answer based on Meme Composer comment:

const sea: string[] = [ "xd", "sd", "ad" ];
const regex: RegExp[] = sea.map((value) => new RegExp(value));

db.paper.find({ "category": { $in: regex } });

Comments

0

Here is simple way to transform /.*/ style regex.

var sea = [ "/xd/", "/sd/", "/ad/" ];

var rx = [];
sea.forEach(function name(value) {
    var v = value.replace(/\//ig,"");
    rx.push(new RegExp(v));
});

db.paper.find({"category": {$in: rx}});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.