1
我试图查询(获取)MongoDB的一个集合的单个字段值,并且我想使用节点js将它存储在一个变量中我不明白我执行查询以便获取特定的字段值并存储到变量中。在MongoDB中使用nodejs获取特定的字段值的查询
1).Node JS
var agegroupQuiz = require('../models/agegroup.js');
exports.agegroupController = function(req,res,next){
try{
var query = {max_age:item}; max_age value is 5 in my mongo database
var age = agegroupQuiz.findOne(query,function(err,data){
if(err){
console.log(err);
}
res.send(data);
});
if(age===5){
console.log(age); //this will show me the value of stored in mongodb database.
return next(err);
}
else{console.log("error in your code");}
}catch(err){
console.log('Error While Saving the result ' +err);
return next(err);
}
}
2).Mongodb架构
This is my age schema inside models (../models/agegroup.js)
module.exports = (function AgeGroupSchema() {
var Schema = mongoose.Schema;
var AgeGroupSchema = new Schema({
max_age:{type:Number} //I want this value is stored into my age variable.
});
var AgeGroup = mongoose.model('agegroups', AgeGroupSchema);
return AgeGroup;
})();
所以,我需要,其获取从MongoDB的特定字段值和存储到一个变量中的查询。
感谢答复BRO:
然后,您可以在您的路线(例如)使用此功能作为中间件 –