使用Mongoose顺序创建文档

时间:2017-08-30 21:30:37

标签: node.js mongodb mongoose

我们的模型看起来像

const NodeSchema = new Mongoose.Schema({
    title: { type: String, default: ''},
    order: { type: Number, min: 0, required: true },
    parentId: { type: String, ref: 'Node', default: null },
});

Mongoose.model('Node', NodeSchema);

我们使用Mongoose验证规则验证parentId是否指向以前添加的文档。这些灯具都在JSON文件中,并且它们被排序,以便在其父级之前不添加任何节点。

然后我们使用:

加载灯具
const fixtures = JSON.parse(Fs.readFileSync(file), 'utf8');
model.create(fixtures, next);

我们的问题是灯具没有按照它们在文件上的顺序加载,验证失败。

除了逐个添加灯具之外(即使使用Async和Bluebird,也会产生非常丑陋的异步代码),有没有办法让Mongoose按顺序添加灯具?

0 个答案:

没有答案