简体   繁体   English

MongoDB / mongoose-嵌套文档未保存

[英]MongoDB/mongoose - Nested documents are not being saved

I've got 4 models, Grid , Container , Row and Column . 我有4个模型, GridContainerRowColumn I need to init a Grid with the rest of the 3 models nested in it. 我需要使用嵌套在其中的3个模型的其余部分来初始化Grid

The problem I'm having is that references to Container , Row and Column are created ( ObjectId s), but the documents themselves aren't saved. 我遇到的问题是创建了对ContainerRowColumn引用( ObjectId ),但是文档本身并未保存。 Only the Grid document is saved. 仅保存Grid文档。

I would expect it to save the rest of the models to their respective collections as well... 我希望它将其他模型也保存到各自的集合中...

What am I missing? 我想念什么?

Here are my models/schemas: 这是我的模型/方案:

const GridSchema = new Schema({
  container: {type: mongoose.Schema.Types.ObjectId, ref: 'Container', required: true}
});

const ContainerSchema = new Schema({
  rows: [{type: mongoose.Schema.Types.ObjectId, ref: 'Row'}]
});

const RowSchema = new Schema({
  columns: [{type: mongoose.Schema.Types.ObjectId, ref: 'Column'}]
});

const ColumnSchema = new Schema({
  modules: [{type: mongoose.Schema.Types.ObjectId, ref: 'Module'}]
});

const Grid = mongoose.model('Grid', GridSchema);
const Container = mongoose.model('Container', ContainerSchema);
const Row = mongoose.model('Row', RowSchema);
const Column = mongoose.model('Column', ColumnSchema);

Here's how I init my grid and saving it: 这是我初始化网格并保存它的方式:

  const grid = new Grid({
    container: new Container({
      rows: [
        new Row({
          column: [
            new Column({
              modules: []
            })
          ]
        })
      ]
    })
  });

  grid.save((err, grid) => {
     console.log(err, grid); // No error is thrown
  });

Ofcourse the nested documents will not be saved, as you are not calling their .save() calls. 当然,嵌套文档不会保存,因为您没有调用它们的.save()调用。

Also instead of creating then like this, create then individually, and then use their references or variables to work with. 另外,不要像这样创建,而是分别创建,然后使用其引用或变量进行处理。 that'll make your work easier and cleaner. 这将使您的工作更加轻松和整洁。

Edit: to specify how to make multiple save at once. 编辑:指定如何一次进行多次保存。

You can do it like this: 您可以这样做:

column = new Column({
    modules: []
});
row = new Row({
    column: [column._id]
});
container = new Container({
    rows: [row._id]
});
grid = new Grid({
    container
});
Promise.all([column.save(), row.save(), container.save(), grid.save()]).then((docs) => {
    //all the docs are conatined in docs, this is success case, i.e. all document gets saved
    console.log(docs);
}).catch((error) => {
    // handle error here, none of the documents gets saved
    // if saving of anyone of the documents fails, it all gets failed
});

Promise.all() is very useful for saving multiple documents like this. Promise.all()对于保存这样的多个文档非常有用。 As Mongodb has no feature to support transactions. 由于Mongodb没有支持交易的功能。

And my apologies for late reply. 我很抱歉为您延迟回复。

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

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