I have data in 2 seperate collections stored in the same MongoDB database. Is there a way to define in the HTTP GET request query string from which collection the data should be retrieved. I use the following JS to process the query:
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);
});
};
};
The App is setup in Node.js using express
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);
In the example you shared, you are declaring a POST method, not a GET. Any case, in a GET method you could send a query parameter that identify which collection you want to use. Image you send a query param called collection
http://myhost/data?collection=1
and you could extract that query param with:
var numberCollection = req.query.collection;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.