简体   繁体   English

如何查询包含特定键/值对的文档

[英]How do I query for documents that contain a specific key/value pair

Using monogo's shell, db.collection.find( {key:value} ) returns exactly what I want, only I want to make this query from a browser using a URL like localhost:8000/api/profiles?key=value 使用monogo的shell, db.collection.find( {key:value} )返回我想要的内容,只是我想使用像localhost:8000/api/profiles?key=value这样的URL从浏览器进行此查询localhost:8000/api/profiles?key=value

I'm trying to use Express, Node and Mongoose. 我正在尝试使用Express,Node和Mongoose。

Here is the code I have: 这是我的代码:

//Get By query route
router.get('/:param', function(req, res){
    var param = req.query(param);
    getProfilebyQuery(function(err, profiles) {
    if(err){
        throw err;
    }
res.json(profiles)
    })
});
//METHODS GO HERE
//These methods are stored as variables and called in the Routes above.
//Get by Query
var getProfilebyQuery = function(param, callback) {
    Iprofile.find(param, callback);
};

What you can't see is "Iprofile" requires my mongoose schemas. 你看不到的是“Iprofile”需要我的猫鼬模式。 I can't tell what I'm doing wrong. 我不知道我做错了什么。

Since you're using query parameters ( difference between req.query and req.params ), you don't need to define /:param . 由于您使用的是查询参数( req.query和req.params之间的差异 ),因此您无需定义/:param You need to use req.query. 您需要使用req.query。

Also you are not passing the param variable to function. 另外,您没有将param变量传递给函数。

//Get By query route
router.get('/', function(req, res){
  var param = req.query;
  getProfilebyQuery(param, function(err, profiles) { //pass param
    if(err){
      throw err;
    }
    res.json(profiles)
  })
});
//METHODS GO HERE
//These methods are stored as variables and called in the Routes above.
//Get by Query
var getProfilebyQuery = function(param, callback) {
  Iprofile.find(param, callback);
};

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

相关问题 如何在jquery / javascript中的键/值数组中的特定点插入键值对? - How do I insert a key value pair at a specific point in a key/value array in jquery/javascript? 如何在ajax javascript中获取数据类型json的特定值键对? - How do I get specific value key pair in ajax javascript for datatype json? 如何使用Javascript搜索特定的JSON键值对? - How do I search for a specific JSON key value pair using Javascript? 在Javascript中,如何检查嵌套在另一个键/值对中的特定键/值对的存在? - In Javascript, how can I check the existence of a specific key/value pair nested inside another key/value pair? 如何在javascript中的顺序中添加key:value对 - How do I add key:value pair in an order in javascript 如何从 MySQL 以键和值对的形式返回数据 - How do I return data as a Key & Value pair from MySQL 如何将键值对添加到 javascript 中的关联数组 - How do i add a key value pair to an asociative array in javascript 如何将键值对与Json字符串分开 - How do I separate key value pair from Json String 如何将 JSON 值转换为键值对 pir - How do I convert JSON values into a key value pair pir 如何从对象数组中的给定键查询包含字段的文档? - How to query the documents that contain field from given key in array of object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM