ฉันกำลังใช้สำเร็จรูป linnovate MEAN เพื่อสร้างแอปพลิเคชัน: https://github.com/linnovate/mean
ด้วย MongoDB ฉันเข้าใจวิธีสืบค้นคอลเลกชันในฐานข้อมูลและรับผลลัพธ์ผ่านทางบรรทัดคำสั่ง เช่น:
db.articles.find({ 'title' : 'hello world'})
ในแอป MEAN พื้นที่ที่ฉันสังเกตเห็นสำหรับการสืบค้นประเภทนี้อยู่ในไฟล์ app/controllers/articles.js
:
/**
* 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);
}
});
};
หากฉันต้องการเพิ่มวิธีส่งคืนรายการอื่นด้วยข้อความค้นหาเฉพาะ ฉันจะดำเนินการอย่างไร นี่คือโค้ดที่ฉันกำลังดำเนินการ:
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);
}
});
}
});
};