Search code examples
mongodbgrailsgrails-orm

MongoDB WildCard query taking too much time using Grails


  • MongoDB v4.0.2
  • Grails 3.3.5

I've more than 20 Millions of records stored in a collection. I'm trying to wildcard search in that collection like as follows...

def personList = Person.collection.find(['vehicleNumber': ['$regex':/.*GJ18AD.*/] ]).sort(["datetime":-1])

Index on Person Collection

db.person.getIndexes()
{
    "v" : 2,
    "key" : {
        "vehicleNumber" : 1
    },
    "name" : "vehicleNumber_1",
    "ns" : "analytics.person",
    "weights" : {
        "numberPlate" : 1
    },
    "default_language" : "english",
    "language_override" : "language",
    "textIndexVersion" : 3
}

Is there any other way for the wildcard search?


Solution

  • There is no changes required in the indexing. But the minor change in the filter Object which I'm passing to the collection.

    Previously, I was using following filter object syntax:

     def personList = Person.collection.find(['vehicleNumber': ['$regex':/.*GJ18AD.*/] ]).sort(['datetime':-1])
    

    Then I've change only the regex in the above syntax:

    def personList = Person.collection.find(['vehicleNumber': ['$regex':'.*GJ18AD.*'] ]).sort(['datetime':-1])
    

    It's works for me in the MongoDB version 4.2.1.