Mongoose.js:通过用户名 LIKE 值查找用户

2022-01-20 00:00:00 mongodb find node.js javascript mongoose

我喜欢通过查找名为 value 的用户在 mongoDb 中查找用户.问题:

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

username: 'peter'

如果用户名是Peter"或PeTER".. 或类似的东西,我找不到它.

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

所以我想像 sql 那样做

So i want to do like sql

SELECT * FROM users WHERE username LIKE 'peter'

希望你们得到我的要求?

Hope you guys get what im askin for?

简短:mongoose.js/mongodb 中的字段 LIKE 值"

推荐答案

对于那些在这里寻找解决方案的人来说:

For those that were looking for a solution here it is:

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

相关文章