Mongoose Schema.prototype.static()函数

Mongoose Schema.prototype.static()函数

Mongoose Schema API.prototype.static() method 是用于Schema对象的。它允许我们使用模式对象为模型定义静态类方法。我们可以在Schema对象上访问static方法来定义静态方法。让我们通过一个示例来理解static()方法。

语法:

schemaObject.static( method, callback );

参数: 此方法接受以下两个参数:

  • method: 用于在模型的类级别上指定方法名称。
  • callback: 用于指定将为该方法执行特定任务的函数。

返回值: 该方法可以根据我们的函数定义返回任何值。

设置Node.js Mongoose模块:

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

npm init

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

npm install mongoose

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

Mongoose Schema.prototype.static()函数

示例1: 下面的示例演示了Mongoose Schema static()方法的功能。在这个示例中,我们定义了一个名为findCustomerByAddress的静态方法,我们正在检索address字段值为Indore的文档,并使用then和catch块处理返回的值作为一个promise。

文件名: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 customerSchema = new mongoose.Schema({ 
    name: String, 
    address: String, 
    orderNumber: Number, 
}); 
  
customerSchema.static('findCustomerByAddress',  
function (address) { 
    return this.find({ address: address }); 
}) 
  
const Customer = 
    connectionObject.model('Customer', customerSchema); 
  
Customer.findCustomerByAddress('Indore').then(result => { 
    console.log(result); 
}).catch(error => console.log(error));

运行程序的步骤: 在项目的根目录中执行以下命令来运行应用程序:

node app.js

输出:

[
  {
    _id: new ObjectId("639ede899fdf57759087a655"),
    name: 'Chintu',
    address: 'Indore',
    orderNumber: 6,
    __v: 0
  }
]

示例2: 下面的示例演示了Mongoose Schema的静态方法 static() 的功能。在这个示例中,我们定义了一个名为 getAll 的静态方法,我们通过匿名异步函数从数据库中获取所有文档,并处理返回的值。

文件名: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 customerSchema = new mongoose.Schema({ 
    name: String, 
    address: String, 
    orderNumber: Number, 
}); 
  
customerSchema.static('getAll', function () { 
    return this.find({}); 
}) 
  
const Customer =  
    connectionObject.model('Customer', customerSchema); 
  
(async () => { 
    const result = await Customer.getAll(); 
    console.log(result); 
})();

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

node app.js

输出:

[
  {
    _id: new ObjectId("639ede899fdf57759087a655"),
    name: 'Chintu',
    address: 'Indore',
    orderNumber: 6,
    __v: 0
  },
  {
    _id: new ObjectId("639ede899fdf57759087a653"),
    name: 'Aditya',
    address: 'Mumbai',
    orderNumber: 2,
    __v: 0
  },
  {
    _id: new ObjectId("639ede899fdf57759087a654"),
    name: 'Bhavesh',
    address: 'Delhi',
    orderNumber: 5,
    __v: 0
  }
]

参考文献: https://mongoosejs.com/docs/api/schema.html#schema_Schema-static

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程