如何在 mongoose 需要验证的情况下在 mongoose 中插入多个文档?
How to inset multiple documents in mongoose with mongoose required validation?
我想在 MongoDB collection 中插入多个文档。我能够通过使用 Model.collection.insert 函数来做到这一点,但是当我插入这些数据时,它 skip/bypass 需要验证。
我试过 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()
我想在 MongoDB collection 中插入多个文档。我能够通过使用 Model.collection.insert 函数来做到这一点,但是当我插入这些数据时,它 skip/bypass 需要验证。
我试过 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()