Expressjs:搜索查询api [英] Expressjs: Search query api

查看:115
本文介绍了Expressjs:搜索查询api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用查询字符串搜索我的用户资料库。



这应该返回所有用户使用类似的用户名kyogron和类似的电子邮件kyogron @ gmail

  GET localhost:3000 / users?username = kyogron& email = kyogron@gmail.com 

这应该返回所有用户:

  GET localhost:3000 / users 

我已经设法处理路由参数,但我被困在优化它:

  app.get('/ users',function(req,res){
//创建一个mongoosejs查询对象
var query = User.find({});

//了解expressjs处理查询的方式:
//?username = kyogron& email = kyogron @ gmail.com
// {username:kyogron,电子邮件:kyogron@gmail.com}
//console.log(req.query);

//这是一个优化搜索查询参数的想法
//这不行不知道为什么我总是得到一个所有用户的数组
//甚至键和值是正确的
Object.keys(req.query).forEach(function(key){
query。 select(key,req.query [key]);
});

//这是我第一次处理参数的方式,这样做!
// if(req.query.username)query.where('username',req.query.username);
// if(req.query.email)query.where('email',req.query.email);

//其余查询
query.select('username','email');
query.exec(function(err,users){
if(err)throw err;
res.json(users);
});

});

这些是我正在争取的问题:


  1. 为什么不重复req.query对象工作?

  2. 如何说使用通配符(例如kyo *)的mongoose

如果有人可以帮助我,会很好:)



编辑



第二个问题可以用$解决:

  if(req.query.username){
query。$ where(function(){
return this.username === req.query.username; //这里我们需要一个正则表达式
});
}

Thos不工作...有人可以给我一个提示吗? / p>

EDIT2:



没有管理$我现在发现

  query.where('username')。regex(); 

我只需要寻找一个搜索正则表达式,寻找类似的单词



EDIT3:



我发现这个线程:如何使用like查询MongoDB?我在mongoosejs组中询问我如何用mongoose



EDIT4:

  if(req.query.username) {
query.where('username')。regex(new RegExp(\ /+ req.query.username +\ /));
}

我几乎得到了。只需要修复这个愚蠢的正则表达式...

解决方案

  app.get('用户',function(req,res){
var query = User.find({});

Object.keys(req.query).forEach(function(key){
query.where(key).regex(new RegExp(req.query [key]));
});

/ *
if(req.query .username){
query.where('username')。regex(new RegExp(req.query.username));
}
if(req.query.email){
query.where('email')。regex(new RegExp(req.query.email));
} * /

query.select('username','email' );
query.exec(function(err,users){
if(err)throw err;
res.json(users);
});

});

第一个没有工作,因为我有一个打字错误(.select()没有.where() )。第二个被发现在一个额外的线程



我仍然不确定所选择的方法。



迭代req.query将允许代码可重用(可能作为前提路由参数功能),但它很容易发生错误


I want to search through my user repository with a query string.

This should return all users with a similar username "kyogron" and similar email "kyogron@gmail"

GET localhost:3000/users?username=kyogron&email=kyogron@gmail.com

This should return all users:

GET localhost:3000/users

I already managed to handle the routing parameters but I am stuck at optimizing it:

app.get('/users', function(req, res) {
    // creates a mongoosejs query object
    var query = User.find({});

    // to understand how expressjs handles queries:
    // ?username=kyogron&email=kyogron@gmail.com
    // { username: "kyogron", email: "kyogron@gmail.com" }
    //console.log(req.query);

    // this was one idea of optimizing the search query parameters
    // this doesn't work don't know why I always get an array of ALL users
    // even the key and value is right
    Object.keys(req.query).forEach(function(key) {
        query.select(key, req.query[key]);
    });

    // this was the way I was first handling the parameters, this works !!
    //if (req.query.username) query.where('username', req.query.username);
    //if (req.query.email) query.where('email', req.query.email);

    // the rest of the query
    query.select('username', 'email');
    query.exec(function(err, users) {
        if (err) throw err;
        res.json(users);
    });

});

These are the problems I am fighting with:

  1. Why doesn't iterating the req.query object work?
  2. How do I say mongoose to use a wildcard (e.g. kyo*)

Would be nice if somebody could help me out :)

Regards

EDIT:

The second issue would be solvable with $where:

    if (req.query.username) {
        query.$where(function() {
            return this.username === req.query.username; // here we need a regex check
        });
    }

Thos doesn't work... Could somebody give me a hint?

EDIT2:

Didn't managed anything with $where... however I now found

query.where('username').regex();

I just have to look for a searching regex which looks for similar words

EDIT3:

I found this thread: How to query MongoDB with "like"? I ask in the mongoosejs group how I could do this with mongoose

EDIT4:

if (req.query.username) {
            query.where('username').regex(new RegExp("\/"+req.query.username+"\/"));
}

I nearly got it. Just have to fix this stupid regex...

解决方案

app.get('/users', function(req, res) {
    var query = User.find({});

    Object.keys(req.query).forEach(function(key) {
        query.where(key).regex(new RegExp(req.query[key]));
    });

    /*
    if (req.query.username) {
        query.where('username').regex(new RegExp(req.query.username));
    }
    if (req.query.email) {
        query.where('email').regex(new RegExp(req.query.email));
    }*/

    query.select('username', 'email');
    query.exec(function(err, users) {
        if (err) throw err;
        res.json(users);
    });

});

The first didn't work because I had a typo (.select() not .where()). The second was found in an extra thread

I am still a bit unsure about the chosen approach.

Iterating req.query would allow to make the code reusable (maybe as precondition routing parameter-function) but it is quite susceptible for errors

这篇关于Expressjs:搜索查询api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆