我一直在查看线程和mongoose文档,但我仍然遗漏了包含子文档的内容。我有快速运行REST api,我想在查找“人员”时从“位置”id给出“位置”详细信息。下面是我的两个架构。
你会看到我正在尝试使用该位置的标题填充people.location ..但最终我想要包含架构中的所有细节。 people.location包含存在的位置的id。
peopleSchema.js
module.exports = function(db) {
return db.model('People', PeopleSchema());
}
function PeopleSchema () {
var Schema = require('mongoose').Schema;
LocationsSchema = new Schema({
title: String
});
console.log(LocationsSchema);
return new Schema({
first_name: String,
last_name: String,
address: {
unit: Number,
address: String,
zipcode: String,
city: String,
region: String,
country: String
},
image: String,
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
location: [{type: Schema.ObjectId , ref: 'Locations'}], // Inheirit store info
employee_number: Number
}, { collection: 'people' });
}
locationSchema.js
module.exports = function(db) {
return db.model('Locations', LocationsSchema());
}
function LocationsSchema () {
var Schema = require('mongoose').Schema;
return new Schema({
title: String,
address: {
unit: Number,
address: String,
zipcode: String,
city: String,
region: String,
country: String
},
current_manager: String, // Inherit person details
alternate_contact: String, // Inherit person details
hours: {
sunday: String,
monday: String,
tuesday: String,
wednesday: String,
thursday: String,
friday: String,
saturday: String,
holidays: String
},
employees: "", // mixin employees that work at this location
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null }
}, { collection: 'locations' });
}
我正带着这个
回来app.get('/people', function (req, res) {
return PeopleModel.find().populate('location').exec(function (err, obj) {
if (!err) {
return res.send(obj);
} else {
return res.send(err);
}
});
});
并且位置字段即将出现null,即使它在那里有id。如果有人能给我任何指针或相关链接,那将是伟大的!如果您需要我发布任何其他内容,请告诉我。