Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude Mongo fields based on regex

My collection has 1000s of fields and I would like to exclude fields that match a certain regex. Several hundred fields are in the form: 'labelXXXX', where XXXX is 4 integers. Is this possible?

(i.e. How would I exclude fields from the collection which match 'labelXXXX' from my query?)

like image 786
yesyoukenn Avatar asked Dec 03 '25 23:12

yesyoukenn


1 Answers

I solved this by using $not in mongodb

    {
    "_id" : "1",
    "name" : "label123"
}

/* 2 */
{
    "_id" : "2",
    "name" : "sample"
}


db.getCollection('things').find({name:{$not:/^label/}})

Output:{ "_id" : "2", "name" : "sample" }

like image 198
Vignesh Avatar answered Dec 05 '25 15:12

Vignesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!