我想在我的 MongoDB 集合中插入多个文档。我可以通过使用 Model.collection.insert 函数来做到这一点,但是当我插入这些数据时,它会跳过/绕过所需的验证。
我尝试过 Model.collection.insert([{data: '1'}, {data: '2'}, {type: '3'}])
但这种方式会跳过或绕过验证。我想要必需的数据字段,并且我根据需要在我的架构中使用了该字段。但这不起作用。
我的架构需要一个字段。
export const SubjectSchema = new mongoose.Schema({
title: { type: String, required: [true, "title field required"] },
groups_id: { type: String },
class_id: { type: String },
meta: { type: Object }
},
{ timestamps: true })
这是我的函数
async createSubject(body) {
let result = SubjectSchema.collection.insert(body)
return result
}
我想要存储多个数据,并且在每条记录中,应需要 title
字段
最佳答案
Model.insertMany([{data: '1'}, {data: '2'}, {type: '3'}])
你可以找到insertMany ref here
但是,您也可以 db.collection.validate()
关于node.js - 如何在 Mongoose 中插入多个文档并需要 Mongoose 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58461552/