Mongoose SchemaType.prototype.default()函数

Mongoose SchemaType.prototype.default()函数

Mongoose的SchemaType.prototype.default()方法是用于SchemaType对象的Mongoose API的方法。它允许我们设置模式中字段的默认值。使用该方法,我们可以为模式中的任何路径提供默认值,可以是字面值或函数的形式。我们提供的值将根据我们为特定字段或路径提供的类型进行转换。让我们通过一个示例了解default()方法。

语法:

schemaTypeObject.path( <path> ).default( <function/value>);

参数: 该方法接受一个参数,如下所述:

  • function/value: 用于指定字段的默认值。

返回值: 该方法返回字段的设置的默认值,并设置字段或SchemaType的默认值路径。

设置Node.js Mongoose模块:

步骤1: 使用以下命令创建一个Node.js应用程序:

npm init

步骤2: 创建NodeJS应用程序后,使用以下命令安装所需的模块:

npm install mongoose

项目结构: 项目结构将如下所示:

Mongoose SchemaType.prototype.default()函数

示例1: 以下示例展示了Mongoose SchemaType的 default() 方法的功能。在此示例中,我们定义了studentSchema,其具有三个属性或字段名称、年龄和rollNumber。我们在rollNumber路径上使用default()方法返回默认值为随机整数。最后,我们可以注意到,当创建一个Student模型的新对象时,我们没有为rollNumber字段提供任何值,而数据库中的默认值即随机整数值反映在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 },
    age: { type: Number },
    rollNumber: { type: Number },
});
 
studentSchema.path('rollNumber')
    .default(Math.ceil(Math.random() * 100))
 
const Student = connectionObject
    .model('Student', studentSchema);
 
Student.create({ name: 'Student1', age: 20 })
.then(newDocument => {
    console.log(newDocument);
})

运行程序的步骤: 要运行应用程序,请从项目的根目录执行以下命令:

node app.js

输出:

注意: rollNumber 字段的值可能会有所不同,因为我使用了 random() 方法,每次执行都会返回一个新的整数值。

{
  name: 'Student1',
  age: 20,
  rollNumber: 9,
  _id: new ObjectId("63a40a1065e8951038a391b1"),
  __v: 0
}

使用Robo3T GUI工具呈现的数据库的GUI表示。

Mongoose SchemaType.prototype.default()函数

示例2: 在这个示例中,我们使用箭头函数返回了rollNumber字段的默认值。同时,我们还为age字段设置了默认值。最后,在创建一个Student模型的新文档对象时,我们只提供了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 },
    age: { type: Number, default: 18 },
    rollNumber: { type: Number },
});
 
studentSchema.path('rollNumber').default(() => {
    return 100 + Math.ceil(Math.random() * 100)
})
 
const Student = connectionObject
    .model('Student', studentSchema);
 
const newStudent = new Student({ name: 'Student2' });
 
newStudent.save().then(document => {
    console.log(document);
})

运行程序的步骤:

要运行该应用程序,请从项目的根目录执行以下命令:

node app.js

输出:

{
    name: 'Student2',
    age: 18,
    _id: new ObjectId("63a40c193c8fe434f7effe5a"),
    rollNumber: 176,
    __v: 0
}

使用Robo3T GUI工具的数据库的GUI表示。

Mongoose SchemaType.prototype.default()函数

参考: https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-default

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程