Mongoose SchemaType.prototype.required() 函数
The Mongoose SchemaType.prototype.required() 方法是用于 SchemaType 对象的 Mongoose API 中使用的。它允许我们在模式的字段上设置验证器。使用此方法,我们可以为 SchemaType 字段设置验证器。我们可以通过示例来理解 required() 方法。
语法:
schemaTypeObject.required( <boolean/function/object> );
参数: 此方法接受两个参数,如下所述:
- required: 用于启用或禁用字段的required属性。
- message: 用于指定错误消息。
返回值: 此方法返回SchemaType对象。
设置Node.js Mongoose模块:
步骤1: 使用以下命令创建Node.js应用程序:
npm init
步骤2: 创建完NodeJS应用程序之后,使用以下命令安装所需的模块:
npm install mongoose
项目结构:
项目的结构将会如下所示:
示例1: 以下示例演示了Mongoose SchemaType required() 方法的功能。在这个示例中,我们定义了一个studentSchema,有三个属性或字段:name、age和rollNumber。最后,name字段是必需的,创建StudentModel对象时必须提供某个值。这就是为什么我们在创建StudentModel对象时没有为name字段提供值,所以会得到验证错误的原因。
文件名: app.js
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const studentSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
rollNumber: { type: Number }
});
const StudentModel =
connectionObject.model('Student', studentSchema);
const doc = new StudentModel({ age: 20, rollNumber: 101 });
doc.save((result => {
console.log(result)
}))
运行程序的步骤: 从项目的根目录执行以下命令来运行应用程序:
node app.js
输出:
Error: Student validation failed: name: Path `name` is required.
示例2:
下面的示例演示了Mongoose SchemaType required() 方法的功能。在这个示例中,我们定义了一个studentSchema,该模式有三个属性或字段:name、age和rollNumber。最后,创建StudentModel对象时,name字段和rollNumber字段是必需的,必须有某个值。
文件名:app.js
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const studentSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
rollNumber: {
type: Number, required: (() => {
return this.age >= 18
})
}
});
const StudentModel =
connectionObject.model('Student', studentSchema);
const document = new StudentModel({ name: 'Student 1',
age: 22, rollNumber: 101 });
document.save().then(document => {
console.log(document);
})
运行程序的步骤: 在项目的根目录下执行以下命令来运行应用程序:
node app.js
输出:
{
name: 'Student 1',
age: 22,
rollNumber: 101,
_id: new ObjectId("63a3630fc9bec4d7e98de925"),
__v: 0
}
示例3: 下面的示例演示了Mongoose SchemaType required()方法的功能。在这个示例中,我们定义了一个studentSchema,它有三个属性或字段:姓名、年龄和学号。最后,姓名字段始终是必需的,但是学号字段在创建StudentModel对象时有条件地必需且必须有某个值。
文件名:app.js
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const studentSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
rollNumber: {
type: Number, required: (() => {
return this.age >= 18
})
}
});
const StudentModel =
connectionObject.model('Student', studentSchema);
const document2 =
new StudentModel({ name: 'Student 2', age: 16 });
document2.save().then(document2 => {
console.log(document2);
})
运行程序的步骤:
要运行应用程序,请从项目的根目录执行以下命令:
node app.js
输出:
{
name: 'Student 2',
age: 16,
_id: new ObjectId("63a36373c69cbe92435273c0"),
__v: 0
}
参考: https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-required