Created
November 26, 2015 16:27
-
-
Save edinella/89193319e2b96b575ebc 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