我有mongodb集合,我需要将高中和大学提取到各自的路线。 我的收藏品看起来像这样
{ “_id”:“1f24ef0c-6f9c-478a-adc9-7c55d7708930”, “highSchool”:“现代时代修道院”, “大学”:“史蒂文斯理工学院” }
我的路线如下: -
const data = require("../data");
const educationData = data.education;
router.get("/highschool", (req, res) => {
educationData.getHighSchool().then((education) => {
res.json(education);
}, () => {
// Not found!
res.sendStatus(404);
});
});
router.get("/", (req, res) => {
educationData.getAllEducation().then((EducationList) => {
res.json(EducationList);
}, () => {
// Something went wrong with the server!
res.sendStatus(500);
});
});
我的“/”路线返回id,大学和高中。但在“/ highschool”中,我只想提取该系列中存在的高中名称。我怎样才能实现这一点。目前我正在使用以下函数来返回数据。
const education = mongoCollections.education;// added for reference
return education().then((educationCollection) => {
return educationCollection.find({}).toArray();
});
},
getHighSchool(){
return education().then((educationCollection)=> {
return educationCollection.highSchool;
});
}
“getAllEducation”函数返回所有内容,但“getHighSchool”没有返回任何内容。
答案 0 :(得分:0)
在getHighScholl
方法
getHighSchool(){
return education().then((educationCollection)=> {
return educationCollection.findOne({}).highSchool;
});
}