我试图完成一些非常简单的事情,但仍然失败了。
我想要做的是,在我的服务器上收到一个get
请求时返回所有文档,但只填充特定的字段。
我的架构如下:
var clientSchema = new Schema({
name:{
type: String,
required: true
},
phone:{
type: String,
required: true
},
email:{
type: String,
required: true
},
address: {
type: String,
required: false
}
});
var orderDetailsSchema = new Schema({
//isn't added to frontend
confirmed:{
type: Boolean,
required: true,
default: false
},
service:{
type: String,
required: true
},
delivery:{
type: String,
required: false
},
payment:{
type: String,
required: false
},
status:{
type: String,
required: true,
default: "new order"
},
});
var orderSchema = new Schema({
reference:{
type: String,
required: true
},
orderdetails: orderDetailsSchema,
client: clientSchema,
wheelspec: [wheelSchema],
invoice:{
type: Schema.Types.ObjectId,
ref: 'Invoice'
}
});
我想要的是只返回client.phone
和client.email
,并且包括orderdetails.status
字段,如果可能的话,还要保留reference
字段。
我已经尝试使用lean()
和populate()
,但是都没有成功。我是否漏掉了非常简单的事情?或者我想要实现的不是那么容易?谢谢!