简体   繁体   English

在查询字符串中执行mongoose查询

[英]perform a mongoose query in querystring

I'm trying to do a simple mongoose query using the query string. 我正在尝试使用查询字符串进行简单的mongoose查询。 This works 这有效

router.get('/', function(req,res) {
    myModel.find({name:"test e"}, function(err,data){
        if(err) console.log(err)
        res.json(data);
    });

});

This doesn't work (I get the whole collection).. 这不起作用(我得到了整个系列)..

router.get('/', function(req,res) {
    console.log(req.query.q)
    myModel.find(req.query.q, function(err,data){
        if(err) console.log(err)
        res.json(data);
    });

});

with this request 有这个要求

/api/myModel?q={name:"test e"}

I don't think it's an url encoding issue since I print the 'q' var and it looks fine server side. 我不认为这是一个url编码问题,因为我打印'q'var并且它看起来很好服务器端。

Side question: if this isn't the standard mode, what's the RESTful standard way to query a db? 附带问题:如果这不是标准模式,查询数据库的RESTful标准方法是什么?

Edit for more general details: I don't need a simple access by id or name like Ashley B suggests, I need a proper search engine for my db (the user, using a graphic web interface, have to be able to query each field separately, potentially) 编辑更多一般细节:我不需要通过ID或名称进行简单访问,如Ashley B建议的,我需要一个适当的搜索引擎用于我的数据库(用户,使用图形Web界面,必须能够查询每个字段另外,可能)

Edit 2: thanks to Blakes Seven I solved my initial problem, but if you know or use a better way to perform a complex query I would happy to discuss. 编辑2:感谢Blakes Seven我解决了我的初始问题,但如果您知道或使用更好的方法来执行复杂的查询,我很乐意讨论。 Maybe I should expose anther resource "/api/seach"? 也许我应该公开其他资源“/ api / seach”?

I think I can answer your first question by answering your second (side question). 我想我可以回答你的第二个问题(附带问题)来回答你的第一个问题。

Let's say you have a User model, normally you'd have an endpoint to get all users, like so: 假设您有一个User模型,通常您有一个端点来获取所有用户,如下所示:

router.get('/users', function(req, res) {
    // find all users
    users.find({}, function(err, data){
        if(err) console.log(err)
        res.json(data);
    });
});

To find a specific user you then have an endpoint like so: 要查找特定用户,您将拥有如下所示的端点:

router.get('/users/:name', function(req, res) {
    // get the user's name from the url and find that user
    users.find({name: req.params.name}, function(err, data){
        if(err) console.log(err)
        res.json(data);
    });
});

So rather than passing the whole query through the query string, you just find use a specific part. 因此,您只需使用特定的部分,而不是通过查询字符串传递整个查询。 Allowing the users to directly access your data with their own queries makes it much much harder to secure. 允许用户使用自己的查询直接访问您的数据会使安全性变得更加困难。

I would recommend you to use some library to parse the querystrings to mongoDB queries. 我建议你使用一些库来解析查询字符串到mongoDB查询。 It would fix your problem and make your code better. 它可以解决您的问题并使您的代码更好。

querymen would help you by transforming /myModel?q=test+e into {name: 'test e'} , giving you full control over the querystring schema. querymen会通过将/myModel?q=test+e转换为{name: 'test e'}来帮助您,让您完全控制查询字符串架构。

var querymen = require('querymen')

// querymen.middleware() defines an express middleware with querystring schema
router.get('/', querymen.middleware({
  q: {
    type: String,
    paths: ['name']
  }
}), function(req, res) {
  console.log(req.querymen.query) // {name: 'test e'}
  myModel.find(req.querymen.query, function(err,data){
    if(err) console.log(err)
    res.json(data);
  });
});

The proper query should look like this: 正确的查询应如下所示:

/api/myModels?name=test%20e

The myModals part is in plural. myModals部分是复数形式。

Check here: How to design RESTful search/filtering? 在这里查看: 如何设计RESTful搜索/过滤?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM