如何使用 mongoose 在 mongodb 中保存子架构
How to save child Schema in mongodb using mongoose
以下是我要保存的订单对象 -
{
shipper:
{ firstName: 'Test ShipName',
address1: '10 Florida Ave',
phone1: '800-123-4567' },
consignee:
{ firstName: 'AAA Manufacturing',
address1: '100 Main Street' },
items:
[
{ length1: 45, weight1: 12, height1: 45, width1: 34 },
{ length2: 42, weight2: 34, height2: 90, width2: 54 }
]
}
关于这样做 -
Order(order).save(function(err, result){
if(err)
throw err;
console.log(result);
});
发货人、收货人正在保存适当的值,但在数据库中(mongodb),项目没有正确保存 -
"items" : [
{
"_id" : ObjectId("54e36e18c59700b513a5309d")
},
{
"_id" : ObjectId("54e36e18c59700b513a5309c")
}
],
以下是我的oderSchema
-
var orderSchema = mongoose.Schema ({
shipper: {type: addressSchema, 'Default':''}},
consignee: {type: addressSchema, 'Default':''} },
items: {type: [itemSchema], 'Default':''} },
});
以下是我的itemSchema -
var itemSchema = mongoose.Schema({
length: {type: Number, required: false },
width: {type: Number, required: false },
height: {type: Number, required: false },
weight: {type: Number, required: false },
});
让我知道我在保存项目信息时做错了什么。
在您的 itemSchema 中,属性是 "length"、"width" 等,但是您要保存的数据的属性在末尾包含数字 "length1"、"length2",等等。您需要删除这些数字。
以下是我要保存的订单对象 -
{
shipper:
{ firstName: 'Test ShipName',
address1: '10 Florida Ave',
phone1: '800-123-4567' },
consignee:
{ firstName: 'AAA Manufacturing',
address1: '100 Main Street' },
items:
[
{ length1: 45, weight1: 12, height1: 45, width1: 34 },
{ length2: 42, weight2: 34, height2: 90, width2: 54 }
]
}
关于这样做 -
Order(order).save(function(err, result){
if(err)
throw err;
console.log(result);
});
发货人、收货人正在保存适当的值,但在数据库中(mongodb),项目没有正确保存 -
"items" : [
{
"_id" : ObjectId("54e36e18c59700b513a5309d")
},
{
"_id" : ObjectId("54e36e18c59700b513a5309c")
}
],
以下是我的oderSchema
-
var orderSchema = mongoose.Schema ({
shipper: {type: addressSchema, 'Default':''}},
consignee: {type: addressSchema, 'Default':''} },
items: {type: [itemSchema], 'Default':''} },
});
以下是我的itemSchema -
var itemSchema = mongoose.Schema({
length: {type: Number, required: false },
width: {type: Number, required: false },
height: {type: Number, required: false },
weight: {type: Number, required: false },
});
让我知道我在保存项目信息时做错了什么。
在您的 itemSchema 中,属性是 "length"、"width" 等,但是您要保存的数据的属性在末尾包含数字 "length1"、"length2",等等。您需要删除这些数字。