I am using the linnovate MEAN boilerplate to build an application: https://github.com/linnovate/mean
With MongoDB I understand how to query a collection on the database and get the results via the command line such as:
db.articles.find({ 'title' : 'hello world'})
In the MEAN app the area I noticed for this type of querying is in the app/controllers/articles.js
file:
/**
* List of Articles
*/
exports.all = function(req, res) {
Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(articles);
}
});
};
If I wanted to add a way of returning another list with a specific query how would I go about that?Here is the code I'm working on:
exports.all = function(req, res) {
Article.find().sort('-created').populate('user', 'name username').exec(function(err, articles) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(articles);
Article.find({ 'category' : 'hello world').sort('-created').populate('user', 'name username').exec(function(err, morearticles) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(morearticles);
}
});
}
});
};
Due to its asynchronous nature, you have to place your second query within the else-statement
of your first.
Please see this questions for further information:
rendering results of multiple DB/mongoose queries to a view in express.js
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.