简体   繁体   English

如何使用猫鼬在mongodb中保存子模式

[英]How to save child Schema in mongodb using mongoose

Following is my order object that I am trying to save - 以下是我要保存的订单对象-

{ 
  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 }
    ]
}

On doing this - 在这样做时-

        Order(order).save(function(err, result){
            if(err)
                throw err;
            console.log(result);
        });

shipper, consignee are saving appropriate values but in database(mongodb), items are not saving properly - 托运人,收货人正在保存适当的值,但在数据库(mongodb)中,项目未正确保存-

"items" : [
    {
        "_id" : ObjectId("54e36e18c59700b513a5309d")
    },
    {
        "_id" : ObjectId("54e36e18c59700b513a5309c")
    }
],

Following is my oderSchema - 以下是我的oderSchema

var orderSchema = mongoose.Schema ({
    shipper: {type: addressSchema, 'Default':''}},      
    consignee: {type: addressSchema, 'Default':''} },
    items: {type: [itemSchema], 'Default':''} },
});

Following is my itemSchema - 以下是我的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 }, 
}); 

Let me know what I am doing wrong in saving the item info. 让我知道我在保存商品信息时做错了什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM