我想在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
字段必须为
答案 0 :(得分:2)
Model.insertMany([{data: '1'}, {data: '2'}, {type: '3'}])
您可以找到ref here
您怎么也可以db.collection.validate()