Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mongoose.js: Find user by username LIKE value

Mongoose.js: Find user by username LIKE value

Problem

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

Problem courtesy of: PeterBechP

Solution

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..
});
Solution courtesy of: PeterBechP

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Mongoose.js: Find user by username LIKE value

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×