Mongoose Query.prototype.select() 函数

Mongoose Query.prototype.select() 函数

Mongoose 是用于MongoDB的对象数据建模(ODM)库。它定义了一个强类型模式,具有默认值和模式验证,后者被映射到MongoDB文档。

Mongoose Query API的select方法 用于确定在从数据库中获取它们时要选择或取消选择哪些文档字段。让我们通过一些示例更多地了解这个方法。

语法:

Query.prototype.select()
JavaScript

参数: 它接受一个参数。

  • arg: 它可以是Object、String或String数组的类型。

返回类型: 返回Query对象。

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

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

mkdir folder_name
cd folder_name
npm init -y
touch main.js
JavaScript

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

npm install mongoose
JavaScript

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

Mongoose Query.prototype.select() 函数

GUI Representation of the Database using MongoDB Compass : 目前,该集合没有数据。

Mongoose Query.prototype.select() 函数

示例1: 在这个示例中,我们将使用Query API的select()方法来取消选择从数据库中获取到的文档中的“age”属性。

文件名: 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 personSchema = new mongoose.Schema({ 
    name: { 
        type: String, 
    }, 
    age: { 
        type: Number, 
    } 
}); 
  
const personsArray = [ 
    { 
        name: 'Luffy', 
        age: 22 
    }, 
    { 
        name: 'Nami', 
        age: 30 
    }, 
    { 
        name: 'Zoro', 
        age: 15 
    } 
] 
  
const Person = mongoose.model('Person', personSchema); 
  
(async () => { 
    await Person.insertMany(personsArray); 
    const query = Person.find().select('-age') 
    const persons = await query; 
    console.log(persons); 
})() 
JavaScript

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

node main.js
JavaScript

输出: 我们可以看到结果中的值保持不变。

Mongoose Query.prototype.select() 函数

使用MongoDB Compass的数据库的GUI表示:

Mongoose Query.prototype.select() 函数

示例2: 在这个示例中,我们将首先在模式的“name”属性中将“select”设置为“false”,但通过使用查询API的select()方法覆盖为“true”。

文件名: 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 personSchema = new mongoose.Schema({ 
    name: { 
        type: String, 
        select: false
    }, 
    age: { 
        type: Number, 
    } 
}); 
  
const personsArray = [ 
    { 
        name: 'Luffy', 
        age: 22 
    }, 
    { 
        name: 'Nami', 
        age: 30 
    }, 
    { 
        name: 'Zoro', 
        age: 15 
    } 
] 
  
const Person = mongoose.model('Person', personSchema); 
  
(async () => { 
    await Person.insertMany(personsArray); 
    const query = Person.find().select('name') 
    const persons = await query; 
    console.log(persons); 
})() 
JavaScript

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

node main.js
JavaScript

输出: 我们可以看到结果中的数值保持不变。

Mongoose Query.prototype.select() 函数

MongoDB Compass使用的数据库的GUI表示:

Mongoose Query.prototype.select() 函数

参考: https://mongoosejs.com/docs/api/query.html#query_Query-select

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册