Skip to content

Instantly share code, notes, and snippets.

@timelf123
Forked from edinella/textLike.js
Created September 12, 2016 18:05
Show Gist options
  • Select an option

  • Save timelf123/467752184cbc70b5d5585e4a2342f4cd to your computer and use it in GitHub Desktop.

Select an option

Save timelf123/467752184cbc70b5d5585e4a2342f4cd 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