Mongoose Query prototype.selected()函数

Mongoose Query prototype.selected()函数

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

Mongoose Query API selected 方法用于确定Mongoose查询中是否进行了任何选择。它会给出一个布尔响应,即如果进行了任何选择,则为“true”,否则为“false”。让我们通过一些示例更好地理解这个。

语法:

Query.prototype.selected()
JavaScript

参数: 它不接受任何参数。

返回类型: 它返回一个布尔值响应。

创建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.selected()函数

使用MongoDB Compass的GUI表示的数据库: 当前,该集合没有数据。

Mongoose Query prototype.selected()函数

示例1: 在这个示例中,我们将使用Query API的selected()方法来检查在Mongoose查询中是否进行了选择。当我们选择从数据库中获取的文档中的”name”属性时,它将返回”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 () => { 
    const query = Person.find().select('name') 
    console.log(query.selected()); 
})()
JavaScript

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

node main.js
JavaScript

结果: 我们看到结果中的值保持不变。

Mongoose Query prototype.selected()函数

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

Mongoose Query prototype.selected()函数

示例2: 在这个示例中,我们将使用Query API的selected()方法来检查Mongoose查询中是否选择了某个属性。由于我们先选择了“name”属性,然后使用Mongoose查询链接技术取消选择,所以它将返回“false”。

文件名: 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 () => { 
    const query = Person.find().select('name').select('-name') 
    console.log(query.selected()); 
})() 
JavaScript

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

node main.js
JavaScript

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

Mongoose Query prototype.selected()函数

使用MongoDB Compass的GUI Represention的数据库:

Mongoose Query prototype.selected()函数

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册