Mongoose.js: Find user by username LIKE value


I like to to go find a user in mongoDb by looking for a user called value. The problem with:

username: 'peter'

is that i dont find it if the username is "Peter", or "PeTER".. or something like that.

So i want to do like sql

SELECT * FROM users WHERE username LIKE 'peter'

Hope you guys get what im askin for?

Short: 'field LIKE value' in mongoose.js/mongodb

4/2/2012 7:30:08 PM

Accepted Answer

Well. Just for people who are lookin for the answer i was i did like this

var name = 'Peter';
model.findOne({name: new RegExp('^'+name+'$', "i")}, function(err, doc) {
  //Do your action here..
3/29/2012 7:52:11 PM

I had problems with this recently, i use this code and work fine for me.

var data = 'Peter';

db.User.find({'name' : new RegExp(data, 'i')}, function(err, docs){

Use directly /Peter/i work, but i use '/'+data+'/i' and not work for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow