数据关联-参考。如何创建多个文档并将其ID推入另一个模型?

时间:2018-06-29 12:13:36

标签: node.js mongodb express mongoose

这是我的模特

var tripSchema = new mongoose.Schema({
image: String,
location: String,
date: Date,
description: String,
author: {
    id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    },
    username: String
    },
pictures: [
        {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Picture"
        }
    ]
});

var pictureSchema = new mongoose.Schema({
image: String,
location: String,
description: String,
author: {
    id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
    },
    username: String
}
});

我使用Pictute.create()可以很好地创建1个文档。现在,我想一次添加多个文档。 我正在尝试将相同的代码放入循环中,但不起作用。 我还有其他方法可以做到吗?

0 个答案:

没有答案