Query.prototype.explain() 如何在Mongoose中工作?
Query.prototype.explain()函数用于设置解释选项,从而使该查询返回详细的执行统计信息,而不是实际的查询结果。
句法:
Query.prototype.explain()
参数:此函数有一个可选的详细参数。
返回值:此函数返回查询对象。
安装mongoose:
npm install mongoose
安装mongoose模块后,您可以使用命令在命令提示符下检查您的mongoose版本。
npm mongoose --version
现在,创建一个文件夹并添加一个文件,例如 index.js,如下所示。
数据库:这里使用的示例数据库如下所示:
项目结构:项目结构将如下所示。
示例 1:
index.js
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
var query = User.find({age: 5}).explain('queryPlanner')
query.exec(function(err, res){
if(err) console.log(err.message)
else console.log(res)
});
index.js
const express = require('express');
const mongoose = require('mongoose');
const app = express()
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
var query = User.find({age: 5}).explain('allPlansExecution')
query.exec(function(err, res){
if(err) console.log(err.message)
else console.log(res)
});
app.listen(3000, function(error ) {
if(error) console.log(error)
console.log("Server listening on PORT 3000")
});
使用以下命令运行index.js文件:
node index.js
输出:
[
{
queryPlanner: {
plannerVersion: 1,
namespace: 'geeksforgeeks.users',
indexFilterSet: false,
parsedQuery: [Object],
queryHash: '3838C5A3',
planCacheKey: '38305F3',
winningPlan: [Object],
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 1,
executionTimeMillis: 0,
totalKeysExamined: 0,
totalDocsExamined: 4,
executionStages: [Object],
allPlansExecution: []
},
serverInfo: {
host: 'Lenovo530S',
port: 27017,
version: '4.2.0',
gitVersion: 'a4b751dcf51dd249c58812b390cfd1c0129c30'
},
ok: 1
}
]
示例 2:
index.js
const express = require('express');
const mongoose = require('mongoose');
const app = express()
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
// User model
const User = mongoose.model('User', {
name: { type: String },
age: { type: Number }
});
var query = User.find({age: 5}).explain('allPlansExecution')
query.exec(function(err, res){
if(err) console.log(err.message)
else console.log(res)
});
app.listen(3000, function(error ) {
if(error) console.log(error)
console.log("Server listening on PORT 3000")
});
使用以下命令运行index.js文件:
node index.js
输出:
Server listening on PORT 3000
[
{
queryPlanner: {
plannerVersion: 1,
namespace: 'geeksforgeeks.users',
indexFilterSet: false,
parsedQuery: [Object],
queryHash: '3838SF3',
planCacheKey: '3238C5F3',
winningPlan: [Object],
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 1,
executionTimeMillis: 0,
totalKeysExamined: 0,
totalDocsExamined: 4,
executionStages: [Object],
allPlansExecution: []
},
serverInfo: {
host: 'Lenovo530S',
port: 27017,
version: '4.2.0',
gitVersion: 'a4b751dcf51sd249c5865812b390cfd1c0129c30'
},
ok: 1
}
]
参考:
https://mongoosejs.com/docs/api/query.html#query_Query-explain