我想从我的Mongoose模式中抽象出一个模型:
AddressSchema = mongoose.Schema(
_type: String
first_name: String
last_name: String
address_line1: String
address_line2: String
zip_code: String
city: String
state: String
phone_number: Number
)
AddressSchema = Schema.AddressSchema
然后在以后做这样的事情:
ShippingChoicesRequestSchema = mongoose.Schema(
retailer: String
products: [ ProductSchema ]
shipping_address:
ref: AddressSchema
type: ObjectId
)
但是,当我发送请求时,它不会创建此嵌套地址文档。我知道我可以通过像shipping_address: [ AddressSchema]
那样做一个数组来做这个,但如果它不是重复的字段呢?
编辑:
请求如下:
{
"retailer": "something",
"products": [...],
"shipping_address": {
"_type": "address",
"first_name": "...",
"last_name": "...",
"address_line1": "...",
"address_line2": "...",
"zip_code": "...",
"city": "...",
"state": "...",
"phone_number": ...
}
}
但是当我在Mongo中查找时,我得到了:
{
"retailer" : "something",
"_id" : ObjectId("526ad0a3c0cff58a54000001"),
"products" : [
{
"variant_choice" : {
"unit_price" : 3999,
"color" : "Black",
"size" : "M",
"_type" : "variant_choice"
},
"quantity" : 1,
"product_id" : "123456",
"_type" : "product"
}
],
"__v" : 0
}
因此地址不会被存储。
答案 0 :(得分:0)
我们在这里定义架构:
AddressSchema = mongoose.Schema(
_type: String
first_name: String
last_name: String
address_line1: String
address_line2: String
zip_code: String
city: String
state: String
phone_number: Number
);
您还需要定义模型:
var AddressModel = mongoose.model('Address', AddressSchema);
然后我们可以将Address作为一个子目录引用:
ShippingChoicesRequestSchema = mongoose.Schema(
retailer: String
products: [ ProductSchema ]
shipping_address: {type: ObjectId, ref: 'Address'}
);