如何使用mongoose在mongodb中保存子Schema

时间:2015-02-17 16:44:19

标签: node.js mongodb express 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 }, 
}); 

让我知道在保存项目信息时我做错了什么。

1 个答案:

答案 0 :(得分:1)

在itemSchema中,属性为“length”,“width”等,但是您保存的数据的属性包含末尾“length1”,“length2”等的数字。您需要删除这些数字