Mangusta.js: Find user by username LIKE value
Lubię szukać użytkownika w mongoDb, szukając użytkownika o nazwie value. Problem z:
username: 'peter'
Jest to, że nie znajdę go, jeśli nazwa użytkownika jest "Peter", lub "PeTER".. albo coś w tym stylu.
Więc chcę zrobić jak sql
SELECT * FROM users WHERE username LIKE 'peter'
Mam nadzieję, że dostaniecie to, o co Proszę?
krótko: "field LIKE value" w języku Mangusta.js / mongodb
11 answers
Cóż. Po prostu dla ludzi, którzy szukają odpowiedzi byłem zrobiłem tak
var name = 'Peter';
model.findOne({name: new RegExp('^'+name+'$', "i")}, function(err, doc) {
//Do your action here..
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-29 19:52:11
Miałem ostatnio problemy z tym, używam tego kodu i działa dobrze dla mnie.
var data = 'Peter';
db.User.find({'name' : new RegExp(data, 'i')}, function(err, docs){
cb(docs);
});
Używać Bezpośrednio /Peter/i
pracować, ale używam {[2] } i nie pracować dla mnie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-02 19:30:41
Powinieneś użyć do tego wyrażenia regularnego.
db.users.find({name: /peter/i});
Bądź jednak ostrożny, że to zapytanie nie używa indeksu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-22 14:13:47
collection.findOne({
username: /peter/i
}, function (err, user) {
assert(/peter/i.test(user.username))
})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-22 14:14:37
db.users.find( { 'username' : { '$regex' : req.body.keyWord, '$options' : 'i' } } )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-26 18:31:10
router.route('/product/name/:name')
.get(function(req, res) {
var regex = new RegExp(req.params.name, "i")
, query = { description: regex };
Product.find(query, function(err, products) {
if (err) {
res.json(err);
}
res.json(products);
});
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-16 22:27:26
Oto Mój kod z expressJS:
router.route('/wordslike/:word')
.get(function(request, response) {
var word = request.params.word;
Word.find({'sentence' : new RegExp(word, 'i')}, function(err, words){
if (err) {response.send(err);}
response.json(words);
});
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-14 03:51:35
Tego właśnie używam.
module.exports.getBookByName = function(name,callback){
var query = {
name: {$regex : name}
}
User.find(query,callback);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-27 06:14:03
Poniższe zapytanie znajdzie dokumenty z wymaganym ciągiem znaków Przypadku, a także o globalnym zasięgu występowania
var name = 'Peter';
db.User.find({name:{
$regex: new RegExp(name, "ig")
}
},function(err, doc) {
//Your code here...
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-01 03:18:53
Mongoose doc for find. mongodb doc for regex.
var Person = mongoose.model('Person', yourSchema);
// find each person with a name contains 'Ghost'
Person.findOne({ "name" : { $regex: /Ghost/, $options: 'i' } },
function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation);
});
Zwróć uwagę na pierwszy argument, który przekazujemy mongoose.funkcja findOne. "{"name": {$regex: / Ghost/, $options: 'i'}}". "nazwa" to pole poszukiwanego dokumentu. "Ghost" jest wyrażeniem regularnym. "i" oznacza dopasowanie niewrażliwe na wielkość liter. Mam nadzieję, że to ci pomoże.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-21 08:08:37
Jeśli chcę odpytywać wszystkie rekordy pod pewnym warunkiem, mogę użyć tego:
if (userId == 'admin')
userId = {'$regex': '.*.*'};
User.where('status', 1).where('creator', userId);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-10 02:14:43