Mongoose Schemas Virtuals

Mongoose Schemas Virtuals

Mongoose Schemas Virtuals 是一个逻辑上存在但不写入 MongoDB 数据库的文档属性。它们不持久化或存储在那里。当我们访问虚拟属性时,Mongoose 调用 get 方法。虚拟属性的 setters 在其他验证之前应用。

语法:

schema({
    virtuals: {
        propertyName: 'Program Logic'
    }
});
JavaScript

参数:

  • propertyName: 它是您想要定义的属性的名称。

安装mongoose模块:

您可以访问链接来安装mongoose模块。您可以使用以下命令来安装此软件包。

npm install mongoose
JavaScript

之后,你只需创建一个文件夹并添加一个名为index.js的文件,要运行该文件,你需要运行以下命令。

node index.js
JavaScript

示例1: 以下示例演示了Mongoose Schema虚拟属性的功能。在这个示例中,我们定义了一个名为fullName的虚拟属性,它将返回一个字符串。我们将使用这个示例模式选项来定义虚拟属性。

// Require the mongoose module 
const mongoose = require('mongoose'); 
  
// Path to our cloud DataBase 
const url = "mongodb://localhost:27017/GFG"
  
// Connecting to database 
mongoose.set('strictQuery', false); 
  
mongoose.connect(url) 
    .then((ans) => { 
        console.log("Connected Successful") 
    }) 
    .catch((err) => { 
        console.log("Error in the Connection") 
    }) 
  
// Calling Schema class 
const Schema = mongoose.Schema; 
  
// Creating Structure of the model 
const schema = new Schema({ 
    firstName: { 
        type: String, 
        require: true, 
    }, 
    lastName: { 
        type: String, 
        require: true, 
    } 
}, 
    { 
        virtuals: { 
            fullName: { 
                get() { 
                    return this.firstName + 
                    ' ' + this.lastName; 
                } 
            } 
        } 
    }); 
  
// Compile our model 
const Person = mongoose.model('Person', schema); 
  
// Create a model 
const person = new Person( 
    { firstName: 'Sam', lastName: 'Snehil' } 
); 
  
// Using the virtuals function 
console.log( 
    'FullName without concatenation: ', 
    person.firstName + ' ' + person.lastName); 
console.log( 
    'Full Name with virtuals: ' + person.fullName 
)
JavaScript

输出:

FullName without concatenation:  Sam Snehil
Full Name with virtuals: Sam Snehil
Connected Successful
JavaScript

示例2: 下面的示例说明了Mongoose模式虚拟字段的功能。在这个示例中,我们将使用virtual方法定义虚拟字段。

// Require the mongoose module 
const mongoose = require('mongoose'); 
  
// Path to our cloud DataBase 
const url = "mongodb://localhost:27017/GFG"
  
// Connecting to database 
mongoose.set('strictQuery', false); 
  
mongoose.connect(url) 
    .then((ans) => { 
        console.log("Connected Successful") 
    }) 
    .catch((err) => { 
        console.log("Error in the Connection") 
    }) 
  
// Calling Schema class 
const Schema = mongoose.Schema; 
  
// Creating Structure of the model 
const schema = new Schema({ 
    name: String, 
    address: String, 
}); 
  
// Creating the model 
schema.virtual('Introduce').get((a, b) => { 
    return this.name + ' is from ' + this.address; 
}) 
    .set((v) => { 
        let temp1 = v.split(' '); 
        this.name = temp1[0]; 
        this.address = temp1[1]; 
    }) 
  
// Compile our model 
const Person = mongoose.model('Person', schema); 
  
// Create a model 
const person = new Person({}); 
person.Introduce = 'Sam NewDelhi'; 
  
// Using the virtuals function 
console.log(person.Introduce)
JavaScript

输出:

Sam is from NewDelhi
Connected Successful
JavaScript

参考资料: https://mongoosejs.com/docs/guide.html#virtuals

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册