简体   繁体   English

保存猫鼬嵌套模式

[英]Saving Mongoose nested schema

I'm having an issue saving nested subdocs - not sure if its because it's not an array or what - docs seem to suggest that nested objects are auto saved but they are not in this case. 我在保存嵌套的子文档时遇到问题-不知道是否是因为它不是数组或其他内容-文档似乎建议嵌套对象是自动保存的,但在这种情况下不是。

A child schema: 子架构:

var Address = new Schema({
  phone: String,
  name: String,
  street: { type: String, required: true }
});

Main schema: 主要架构:

var Order = new Schema({
  code: {
    type: String
  },
  address: {
    type: Schema.Types.ObjectId,
    ref: "Address"
  }
});

Neither of these will work. 这些都不起作用。

Create doc doesn't throw errors but subdoc is not saved 创建文档不会引发错误,但不会保存子文档

var a = new Address({ phone: 000 });

var o = new Order({ address: a }).save();

This gives a Cast to ObjectId failed error: 这会导致Cast to ObjectId失败错误:

var o = new Order({ address: { phone: 000 } }).save();

The only way this seems to work is by saving the subdocs first which I'd like to avoid as I have multiple addresses so it's a bit messy. 这似乎可行的唯一方法是先保存子文档,由于我有多个地址,因此我要避免,因此有点混乱。

It's weird that I have never encountered this issue - any ideas? 我从未遇到过这个问题很奇怪-有什么想法吗?

Ok. 好。 Evidently we cannot use subdocs without an array. 显然,没有数组我们就不能使用子文档。 See SO post Embedded document without Array? 看到没有数组的嵌入式文档吗? and bug thread explaining reasoning: https://github.com/LearnBoost/mongoose/pull/585 和解释推理的错误线程: https//github.com/LearnBoost/mongoose/pull/585

The address object in your main schema should be of type Schema.Types.Mixed. 主模式中的地址对象应为Schema.Types.Mixed类型。 You're specifying that address should be an ObjectId, which is why you're getting that Cast to ObjectId error. 您指定的地址应该是一个ObjectId,这就是为什么出现Cast to ObjectId错误的原因。

See http://mongoosejs.com/docs/schematypes.html . 参见http://mongoosejs.com/docs/schematypes.html

Example: 例:

var Order = new Schema({
  code: {
    type: String
  },
  address: {
    type: Schema.Types.Mixed,
    ref: "Address"
  }
});

Ok. 好。 If you only need a field with sub-fields no other schema needed, you can go this way: 如果只需要一个带有子字段的字段,而无需其他模式,则可以采用以下方式:

var Order = new Schema({
  code: {
    type: String
  },
  address: {
    phone: String,
    name: String,
    street: { type: String, required: true }
  }
});

To make it save the address schema all you need to do in your function that will save the order is this: var Address = mongoose.model('Address'); 要使其保存地址模式,您需要在函数中保存顺序的所有步骤是:var Address = mongoose.model('Address'); var Order = mongoose.model('Order'); var Order = mongoose.model('Order');

export.createAddress =  function( req, res) {
   var address =  new Address({phone: 000});
    address.save(callback function);
};

for the Order schema all you need do is: 对于Order模式,您需要做的是:

export.createOrder =  function( req, res){
   var order = new Order(req.body);
    order.address = req.address;
    order.save(callback function);
}; 

please note this is an example using node, express 请注意,这是一个使用节点的示例,表示

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

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