Mongoose 选择猫鼬中的项目
我有一个猫鼬模式是这样的Mongoose 选择猫鼬中的项目,mongoose,selection,Mongoose,Selection,我有一个猫鼬模式是这样的 first_name : String, other_names : String, last_name : String, gender : String, title : String, employment :{ office : String, lo
first_name : String,
other_names : String,
last_name : String,
gender : String,
title : String,
employment :{
office : String,
location : String
},
hobbies :[{
hobbied_id : Schema.Types.ObjectId
}],
contact :[{
address : String,
city : String
}]
当我试图通过mongoose从mongodb中提取信息时,我使用了
Person.find({'exployment.office':'Greenway'})
.exec(function(err, result){
console.log(result);//<- this gives expected result
console.log(result.employment.office);//<- this produces error of undefined
console.log(result.contact.address);//<- this produces error
});
Person.find({'exployment.office':'Greenway'})
.exec(函数(错误、结果){
console.log(result);//result
是一个文档数组,而不仅仅是一个。contact是一个数组,但employment不是一个数组。我如何才能获得上面所述的详细信息,知道吗?您可以迭代结果数组并控制台所有值,或者只执行result[0].employment.office
@AnthonyWinzlet谢谢,我会尝试一下。它是否也适用于作为数组的联系人?或者它们是解决问题的不同方法?