我有2个单独的集合中的数据存储在同一个MongoDB数据库中。有没有办法在HTTP GET请求中定义查询字符串,从哪个集合中检索数据。 我使用以下JS来处理查询:
const findDocuments = function(db, callback) {
const collection = db.collection('THIS should come from the query string');
collection.find(query).toArray(function(err, docs) {
assert.equal(err, null);
callback(docs);
});
};
};
使用快递
在Node.js中设置应用程序dataRouter.route('/data')
.post(function(req, res){
var query =req.query;
//console.log(query);
getResult(query, function(data){
console.log('query done');
//console.log(data);
res.json(data);
});
module.exports = {
query: query
};
});
app.use('/api', dataRouter);
答案 0 :(得分:0)
在您分享的示例中,您声明的是POST方法,而不是GET。 无论如何,在GET方法中,您可以发送一个查询参数来标识您要使用的集合。 您发送一个名为collection
的查询参数的图像http://myhost/data?collection=1
您可以使用以下命令提取该查询参数:
var numberCollection = req.query.collection;