-
-
Save timelf123/467752184cbc70b5d5585e4a2342f4cd to your computer and use it in GitHub Desktop.
MongoDB Query similar to SQL "LIKE"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function textLike(str) { | |
| var escaped = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | |
| return new RegExp(escaped, 'i'); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongoose = require('mongoose'); | |
| var textLike = require('./textLike.js'); | |
| var User = mongoose.model('User'); | |
| User.find({name: textLike('ezequias')}, function(err, docs) { | |
| // | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment