Skip to content

Instantly share code, notes, and snippets.

@edinella
Created November 26, 2015 16:27
Show Gist options
  • Save edinella/89193319e2b96b575ebc to your computer and use it in GitHub Desktop.
Save edinella/89193319e2b96b575ebc to your computer and use it in GitHub Desktop.
MongoDB Query similar to SQL "LIKE"
module.exports = function textLike(str) {
var escaped = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
return new RegExp(escaped, 'i');
};
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