我試圖隱藏我的REST服務器的GET輸出上的某些字段。我有2個模式,都有一個字段可以將來自彼此的相關數據嵌入到GET中,所以/人會返回他們工作的位置列表,並獲取在那裏工作的位置列表。但是,這樣做會增加一個person.locations.employees字段,然後再次列出員工,這顯然不是我想要的。那麼如何在顯示輸出之前從輸出中刪除該字段?謝謝大家,讓我知道你是否需要更多信息。在貓鼬/節點REST服務器中隱藏嵌入的文檔
/********************
/GET :endpoint
********************/
app.get('/:endpoint', function (req, res) {
var endpoint = req.params.endpoint;
// Select model based on endpoint, otherwise throw err
if(endpoint == 'people'){
model = PeopleModel.find().populate('locations');
} else if(endpoint == 'locations'){
model = LocationsModel.find().populate('employees');
} else {
return res.send(404, { erorr: "That resource doesn't exist" });
}
// Display the results
return model.exec(function (err, obj) {
if (!err) {
return res.send(obj);
} else {
return res.send(err);
}
});
});
這是我的GET邏輯。所以我一直試圖在填充函數之後使用mongoose中的查詢函數來嘗試並過濾掉這些引用。這是我的兩個模式。
peopleSchema.js
return new Schema({
first_name: String,
last_name: String,
address: {},
image: String,
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
locations: [{ type: Schema.ObjectId, ref: 'Locations' }],
employee_number: Number
}, { collection: 'people' });
locationsSchema.js
return new Schema({
title: String,
address: {},
current_manager: String, // Inherit person details
alternate_contact: String, // Inherit person details
hours: {},
employees: [{ type: Schema.ObjectId, ref: 'People' }], // mixin employees that work at this location
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null }
}, { collection: 'locations' });