Node.js 如何在开发模式下优化MongoDB查询以获取大量记录?

Node.js 如何在开发模式下优化MongoDB查询以获取大量记录?,node.js,mongoose,Node.js,Mongoose,我的mongodb中有10000条记录,当我试图获取数据并对该数据应用一些过滤器并将响应发送回客户端时,完成查询并将响应发送回客户端需要14或15秒。如何优化查询? 我正在节点应用程序中使用mongoosejs库。 这是我的密码 const mongoose = require('mongoose') const { Schema } = mongoose const StaffSchema = new Schema({ deptName: {type:String}, numbe

我的mongodb中有10000条记录,当我试图获取数据并对该数据应用一些过滤器并将响应发送回客户端时,完成查询并将响应发送回客户端需要14或15秒。如何优化查询? 我正在节点应用程序中使用mongoosejs库。 这是我的密码

const mongoose = require('mongoose')
const { Schema } = mongoose

const StaffSchema = new Schema({
   deptName: {type:String},
   numberOfMembers: {Number}
})

const CollegeSchema = new Schema({
  collegeId:{type:Number},
  collegeName:{type:Number},
  numberOfDepartments: {type:Number},
  founder: {type:String},
  website: {type:String},
  university: {type:String},
  telephone: {type:Number}
  numberOfCoursesAvailable:{type:Number},
  staff: [StaffSchema]
  country: {type:String}
})

module.exports = mongoose.model('College', CollegeSchema, 'colleges')
我正在按关键字筛选大学,可以是大学,也可以是国家

let totalColleges = []
if(country) {
totalColleges = College.find({ country })
} 
if(university) {
 totalColleges = College.find({ university })
}
要获取大量数据(如10000条记录),需要花费大量时间 我们可以选择在生产模式中进行集群。
请告诉我如何在开发模式下对其进行优化。

您所能做的最好的事情是在您用于在查询中更频繁地查询的字段上添加索引

进一步限制recordsPagination的数量和/或限制documentProjection中要返回的字段可用于优化性能,但这些取决于您的要求

还可以查看以下链接:

->

->

希望这有助于: