I'm very new to MongoDB and regex so please bear with me. The MongoDB documentation has an example for searching with regular expressions:
http://docs.mongodb.org/manual/reference/operators/#_S_regex
However, it only shows how to search the values of predetermined keys. I'm unsure how to write a query to search all values in a collection, if anyone can point it out that would be great.
If you didn't care about size of DB (or performance), you could potentially use a mapReduce function to de-normalize the data into a structure which is more easily searched. But, this will significantly increase the size of the DB as you'll duplicate all data for the collection.
Pseudo-code:
// recursive
// building a full property path
for(var key in obj) {
emit(this._id, { orig: obj[key], src: path + "." + key });
// recursive call emitChildren(path, key)
}
Depending on the complexity of the data, it could be reasonable.
Or, you might be able to use $where to do a regex on each key/value of all documents (as you can execute arbitrary JavaScript when using $where).
No option is going to perform well (even a built in option) nor play to the strengths of mongoDB if you're running a regular expression on every value in every document in a collection.
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.