Mongoose 模式实例方法

Mongoose 模式实例方法

Mongoose 是适用于Node.js环境的MongoDB对象建模和处理工具。 Mongoose模式实例方法 指的是Mongoose文档使用的方法。这些方法可以是内置的也可以是自定义的。

创建Node应用程序并安装Mongoose:

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

mkdir folder_name
cd folder_name
npm init -y

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

npm install mongoose

项目结构:

它将看起来像以下这样。

Mongoose 模式实例方法

示例1: 在这个示例中,我们将创建一个自定义的mongoose实例方法来显示所有类型为“dog”的文档。

文件名:main.js

const mongoose = require('mongoose') 
  
// Database connection 
mongoose.connect('mongodb://localhost:27017/query-helpers', { 
    dbName: 'event_db', 
    useNewUrlParser: true, 
    useUnifiedTopology: true
}, err => err ? console.log(err) : console.log('Connected to database')); 
  
const animalSchema = new mongoose.Schema( 
    { name: String, type: String }, { 
      
    // Assign a function to the "methods" object of our  
    // animalSchema through schema options. 
    // By following this approach, there is no need to  
    // create a separate TS type to define the type  
    // of the instance functions. 
    methods: { 
        findSimilarTypes(cb) { 
            return mongoose.model('Animal').find( 
                { type: this.type }, cb); 
        } 
    } 
} 
); 
  
const Animal = mongoose.model("Animal", animalSchema); 
  
const animals = [ 
    { 
        name: 'bond', 
        type: 'dog'
    }, 
    { 
        name: 'cavine', 
        type: 'cat'
    } 
] 
  
const dog = new Animal({ type: 'dog' }); 
  
Animal.insertMany(animals, (err, res) => { 
    dog.findSimilarTypes((err, dogs) => { 
        console.log(dogs); 
    }); 
}) 

运行应用程序的步骤:

从项目的根目录中使用以下命令来运行应用程序:

node main.js

输出:

Mongoose 模式实例方法

示例2: 在这个示例中,我们将创建一个自定义的mongoose实例方法来显示所有类型为“cat”的文档。

文件名:main.js

const mongoose = require('mongoose') 
  
// Database connection 
mongoose.connect('mongodb://localhost:27017/query-helpers', { 
    dbName: 'event_db', 
    useNewUrlParser: true, 
    useUnifiedTopology: true 
}, err => err ? console.log(err) : console.log('Connected to database')); 
  
const animalSchema = new mongoose.Schema({ name: String, type: String }, { 
  
        // Assign a function to the "methods" object  
        // of our animalSchema through schema options. 
        // By following this approach, there is no need  
        // to create a separate TS type to define the type  
        // of the instance functions. 
        methods: { 
            findSimilarTypes(cb) { 
                return mongoose.model('Animal').find({ type: this.type }, cb); 
            } 
        } 
    } 
); 
  
const Animal = mongoose.model("Animal", animalSchema); 
  
const animals = [ 
    { 
        name: 'bond', 
        type: 'dog'
    }, 
    { 
        name: 'cavine', 
        type: 'cat'
    } 
] 
  
const cat = new Animal({ type: 'cat' }); 
  
Animal.insertMany(animals, (err, res) => { 
    cat.findSimilarTypes((err, cats) => { 
        console.log(cats);  
    }); 
}) 

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

node main.js

输出:

Mongoose 模式实例方法

参考: https://mongoosejs.com/docs/guide.html#methods

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程