在mongoose /节点REST服务器中隐藏embedded的文档

我试图隐藏我的REST服务器的GET输出上的某些字段。 我有两个模式,都有一个字段embedded从GET彼此相关的数据,所以得到/人们会返回他们的工作地点列表,并获得位置列表返回谁在那里工作。 但是,这样做会增加一个person.locations.employees字段,然后再列出员工,这显然是我不想要的。 那么如何在显示之前从输出中删除该字段? 谢谢大家,让我知道如果你需要更多的信息。

/******************** / 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' }); 

您应该使用select()方法指定要获取的字段。 您可以通过执行以下操作来完成此操作:

 if( endpoint == 'people' ){ model = PeopleModel.find().select('locations').populate('locations'); } else if( endpoint == 'locations' ){ model = LocationsModel.find().select('employees').populate('employees'); } // ... 

您可以通过用空格分隔来select更多的字段,例如:

 PeopleModel.find().select('first_name last_name locations') ... 

select是正确的答案,但它也可能有助于在您的架构中指定它,以便您保持API的一致性,我发现它帮助我不记得在任何地方执行对该对象的查询。

您可以在架构中将某些字段设置为永不返回,方法是在架构字段上使用select: true|false属性。

更多细节可以在这里find: http : //mongoosejs.com/docs/api.html#schematype_SchemaType-select

解!

因为这对我来说很难find我要把这个留给别人。 为了“取消”填充项目,只需在“select”前添加“ – ”。 例:

 PeopleModel.find().populate({path: 'locations', select: '-employees'}); 

而现在locations.employee的将被隐藏。

如果你记得SQL日子,SELECT对被查询的表进行限制。 限制是来自关系模型的原始操作之一,并且随着关系模型的发展,它仍然是一个有用的function。 等等等等等等。

在mongoose中,Query.select()方法允许你用一些额外的特性来执行这个操作。 特别是,您不仅可以指定要返回的属性(列),还可以指定要排除的属性。

所以这里是例子:

 function getPeople(req,res, next) { var query = PeopleModel.find().populate({path: 'locations', select: '-employees'}); query.exec(function(err, people) { // error handling stuff // process and return response stuff }); } function getLocations(req,res, next) { var query = LocationModel.find().populate({path: 'employees', select: '-locations'}); query.exec(function(err, people) { // error handling stuff // processing and returning response stuff }); } app.get('people', getPeople); app.get('locations', getLocations); 

直接来自Mongoose Docs:

转到http://mongoosejs.com/docs/populate.html并search“查询条件和其他选项”

查询条件和其他选项

如果我们想根据他们的年龄填充我们的粉丝arrays,select他们的名字,最多只能返回5个粉丝?

 Story .find(...) .populate({ path: 'fans', match: { age: { $gte: 21 }}, select: 'name -_id', options: { limit: 5 } }) .exec() 

我只想说,为了简化终点,您可以通过这种方式避开端点定义。 但是,一般来说这种dispacher模式是没有必要的,并且在使用Express进行开发的时候可能会在开发时带来问题。