I have following mongodb document with 800 millions documents.
Customer
{
"lastName" : "LAST",
"firstName" : "FIRST"
}
I have following index
db.customer.createIndex( {lastName: 1, firstName: 1},{collation: { locale: "en_US", strength: 1}})
If I do search based on lastName and firstName, then its fast and getting result with in 10 ms.
db.customer.find({lastName:"LAST" ,firstName:"FIRST"}).collation({ locale: 'en_US', strength: 1 })
I am trying to find all documents that "AS" as the characters in the last name. But its taking more than 100 seconds to return the response. I tried following options but all taking more than 100 seconds. Does mongoDB really does like sql kind of like operation ('%AS%')
1)
db.customer.find({lastName:{"$regex": "AS"} ,firstName:"FIRST"}).collation({ locale: 'en_US', strength: 1 })
2)
db.customer.find({lastName:{"$regex": "/AS/"} ,firstName:"FIRST"}).collation({ locale: 'en_US', strength: 1 })
3)
db.customer.find({lastName:{"$regex": "^/AS/"} ,firstName:"FIRST"}).collation({ locale: 'en_US', strength: 1 })
try use prefix on your regex to allow MongoDB use range
db.customer.find({lastName:{"$regex": "/^AS/"} ,firstName:"FIRST"}).collation({ locale: 'en_US', strength: 1 })
A regular expression is a “prefix expression” if it starts with a caret (^) or a left anchor (\\A), followed by a string of simple symbols. For example, the regex /^abc. / will be optimized by matching only against the values from the index that start with abc. Additionally, while /^a/, /^a. /, and /^a. $/ match equivalent strings, they have different performance characteristics. All of these expressions use an index if an appropriate index exists; however, /^a. /, and /^a.*$/ are slower. /^a/ can stop scanning after matching the prefix.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.