Definition定义
$queryStats
Warning
The $queryStats aggregation stage is unsupported and is not guaranteed to be stable in a future release. Don't build functionality that relies on a specific output format of this stage, since the output may change in a future release.$queryStats聚合阶段不受支持,也不能保证在未来的版本中稳定。不要构建依赖于此阶段特定输出格式的功能,因为输出可能会在未来的版本中发生变化。
Returns runtime statistics for recorded queries.返回已记录查询的运行时统计信息。
$queryStats collects and reports metrics for 集合并报告aggregate(), find(), and distinct() queries. aggregate()、find()和distinct()查询的指标。$queryStats does not collect information for queries that use Queryable Encryption.不集合使用可查询加密的查询的信息。
Requirements需求
The $queryStats stage is enabled on deployments hosted on MongoDB Atlas with a cluster tier of at least M10.$queryStats阶段在MongoDB Atlas上托管的部署上启用,集群层至少为M10。
To run the 要运行$queryStats stage, your pipeline must meet the following requirements:$queryStats阶段,您的管道必须满足以下要求:
The pipeline must be run on the管道必须在admindatabase.admin数据库上运行。$queryStatsmust be the first stage in the pipeline.必须是管道中的第一阶段。
Syntax语法
db.adminCommand( {
aggregate: 1,
pipeline: [
{
$queryStats: {
transformIdentifiers: {
algorithm: <string>,
hmacKey: <binData> /* subtype 8 - used for sensitive data */
}
}
}
],
cursor: { }
} )
Important
Command Fields命令字段
$queryStats takes the following fields:采用以下字段:
transformIdentifiers | $queryStats output.$queryStats输出指定其他转换选项。 | ||
transformIdentifiers.algorithm | transformIdentifiers objecttransformIdentifiers对象,则必需 | algorithm value is hmac-sha-256.algorithm值是hmac-sha-256。 | |
transformIdentifiers.hmacKey | transformIdentifiers objecttransformIdentifiers对象,则需要 | binData |
Access Control访问控制
If your deployment enforces access control, the user running 如果您的部署强制执行访问控制,则运行$queryStats must have the following permissions:$queryStats的用户必须具有以下权限:
To run要运行不带$queryStatswithout thetransformIdentifiersoption, the user must have thequeryStatsReadprivilege action.transformIdentifiers选项的$queryStats,用户必须具有queryStatsRead权限操作。To run要使用$queryStatswith thetransformIdentifiersoption, the user must have the both thequeryStatsReadandqueryStatsReadTransformedprivilege actions.transformIdentifiers选项运行$queryStats,用户必须同时具有queryStatsRead和queryStatsReadTransformed权限操作。
The built-in 内置的clusterMonitor role provides the queryStatsRead and queryStatsReadTransformed privileges. clusterMonitor角色提供queryStatsRead和queryStatsReadTransformed权限。The following example grants the 以下示例授予clusterMonitor role on the admin database:admin数据库上的clusterMonitor角色:
db.grantRolesToUser(
"<user>",
[ { role: "clusterMonitor", db: "admin" } ]
)Behavior行为
The following sections describe behavioral details of the 以下部分描述了$queryStats stage.$queryStats阶段的行为细节。
How $queryStats Tracks Query Statistics$queryStats如何跟踪查询统计信息
Statistics for the $queryStats stage are tracked in a virtual collection that is stored in-memory. The memory limit for the virtual collection is 1% of the system's total memory.$queryStats阶段的统计信息在存储在内存中的虚拟集合中跟踪。虚拟集合的内存限制为系统总内存的1%。
How $queryStats Groups Returned Documents$queryStats如何对返回的文档进行分组
$queryStats groups queries with common properties into the same output document. The resulting document is called a query stats entry.将具有共同属性的查询分组到同一个输出文档中。生成的文档称为查询统计条目。
$queryStats groups similar queries together by normalizing user-provided field values to their data types. 通过将用户提供的字段值规范化为其数据类型,将类似的查询分组在一起。For example, a filter specified as 例如,指定为{ item: 'card' } is normalized to { item : '?string'}. { item: 'card' }的筛选器被规范化为{ item : '?string'}。$queryStats also normalizes the values of some query options like 还规范了一些查询选项(如hint and comment.hint和comment)的值。
$queryStats preserves literal values for options like 保留readConcern and readPreference.readConcern和readPreference等选项的文字值。
For the complete list of options included in a query stats entry, see find Command Query Shape.有关查询统计条目中包含的完整选项列表,请参阅查找命令查询形状。
How $queryStats Transforms Data Using transformIdentifiers$queryStats如何使用转换标识符转换数据
When an HMAC key is specified to the 当为transformIdentifiers option, $queryStats uses the HMAC key to apply an HMAC-SHA-256 hash function on the following data:transformIdentifiers选项指定HMAC键时,$queryStats使用HMAC键对以下数据应用HMAC-SHA-256哈希函数:
Document field names文档字段名称Collection names集合名称Database names数据库名称
$queryStats does not apply the HMAC transformation to the following data:不将HMAC转换应用于以下数据:
MQL keywords such as operator names (for example,MQL键,如运算符名称(例如$gte).$gte)。Parameter names such as the参数名称,如partitionByparameter in$setWindowFields.$setWindowFields中的partitionBy参数。Field values.字段值。$queryStatsnormalizes field values in a query to their data types (such as number or string) when the query is recorded.$queryStatsnever stores field values that contain user data.$queryStats在记录查询时将查询中的字段值标准化为其数据类型(如数字或字符串)。$queryStats从不存储包含用户数据的字段值。
For an example of transformed output, see Transformed Example.有关转换输出的示例,请参阅转换示例。
$queryStats Log Entries$queryStats日志条目
MongoDB records MongoDB在部署日志中记录$queryStats operations in the deployment logs. $queryStats操作。By default, MongoDB only logs the invocation of 默认情况下,MongoDB只记录$queryStats operations, not the operation's output. $queryStats操作的调用,而不是操作的输出。For 对于包含$queryStats operations that include the transformIdentifiers option, you can specify whether the transformed output is included in the log entry.transformIdentifiers选项的$queryStats操作,您可以指定转换后的输出是否包含在日志条目中。
To learn how to control 要了解如何控制$queryStats logging behavior, see Toggle $queryStats Log Output.$queryStats日志记录行为,请参阅切换$queryStat日志输出。
Change Streams更改流
Query stats for change streams are updated when one of these events occur:当以下事件之一发生时,更改流的查询统计信息会更新:
Query stats reported for change streams have these behaviors:为变更流报告的查询统计数据具有以下行为:
Execution metrics such astotalExecMicroscontain information for the most recent operation (cursor creation,getMore, or cursor close).totalExecMicros等执行指标包含最近操作(游标创建、getMore或游标关闭)的信息。Internal内部getMoreoperations increment theexecCountmetric.getMore操作增加execCount度量。firstResponseExecMicrosandtotalExecMicrosare always the same because stats are collected and updated for eachgetMoreoperation.firstResponseExecMicros和totalExecMicros总是相同的,因为每次getMore操作都会集合和更新统计数据。When the cursor closes,当游标关闭时,lastExecutionMicrosis 0.lastExecutionMicros为0。
Output输出
$queryStats returns an array of query stats entries. Some query stats entry properties contain literal values, and some properties are normalized to group common queries.返回一个查询统计条目数组。一些查询统计条目属性包含文字值,一些属性被规范化以对常见查询进行分组。
Query stats entries contain the following top-level documents:查询统计条目包含以下顶级文档:
key |
|
asOf | $queryStats read this entry from the $queryStats virtual collection. $queryStats从$queryStat虚拟集合中读取此条目的UTC时间。asOf does not necessarily return the same UTC time for each result. asOf不一定为每个结果返回相同的UTC时间。 |
metrics |
Each document in the output array contains the following fields:输出数组中的每个文档都包含以下字段:
key | |||
key.queryShape | |||
key.client | |||
key.client.application | |||
key.client.driver | |||
key.client.driver.name | mongosh and nodejs.mongosh和nodejs。 | ||
key.client.driver.version | |||
key.client.os | |||
key.client.os.type | |||
key.client.os.name | |||
key.client.os.architecture | arm64 and x86_64.arm64和x86_64。 | ||
key.client.os.version | |||
key.readConcern | |||
key.collectionType | |||
key.hint | |||
key.batchSize | 101 documents or 16 mebibytes (MiB) worth of documents. 101个文档或价值16兆字节(MiB)的文档中的较小者。batchSize can enforce a smaller limit than 16 MiB, but not a larger one. batchSize可以强制执行小于16 MiB的限制,但不能强制执行更大的限制。batchSize is the lesser of batchSize documents or 16 MiB worth of documents.batchSize是batchSize文档或价值16 MiB的文档中的较小值。 | ||
key.comment | |||
key.maxTimeMS | maxTimeMS | ||
key.noCursorTimeout | noCursorTimeout option associated with the keynoCursorTimeout选项 | ||
key.readPreference | |||
key.apiVersion | |||
key.apiStrict | apiStrict parameter value associated with the key. See Stable API Parameters.apiStrict参数值。请参阅稳定API参数。 | ||
key.apiDeprecationErrors | apiDeprecationErrors parameter value associated with the key. See Stable API Parameters.apiDeprecationErrors参数值。请参阅稳定API参数。 | ||
keyHash | key. Each unique keyHash value corresponds to a unique entry in the $queryStats memory store.key中值的哈希表示。每个唯一的keyHash值对应于$queryStats内存存储中的一个唯一条目。 | ||
queryShapeHash |
| ||
metrics | |||
metrics.lastExecutionMicros | NumberLong | ||
metrics.execCount | NumberLong | ||
metrics.keysExamined | |||
metrics.keysExamined.sum | |||
metrics.keysExamined.max | NumberLong | ||
metrics.keysExamined.min | NumberLong | ||
metrics.keysExamined.sumOfSquares | NumberDecimal |
| |
metrics.docsExamined | |||
metrics.docsExamined.sum | |||
metrics.docsExamined.max | NumberLong | ||
metrics.docsExamined.min | NumberLong | ||
metrics.docsExamined.sumOfSquares | NumberDecimal |
| |
metrics.hasSortStage |
| ||
metrics.usedDisk |
| ||
metrics.fromMultiPlanner |
| ||
metrics.fromPlanCache |
| ||
metrics.totalExecMicros |
| ||
metrics.totalExecMicros.sum | NumberLong | ||
metrics.totalExecMicros.max | NumberLong | ||
metrics.totalExecMicros.min | NumberLong | ||
metrics.totalExecMicros.sumOfSquares | NumberDecimal | sumOfSquares value indicates high variance in query execution times.sumOfSquares值高表示查询执行时间差异大。 | |
metrics.firstResponseExecMicros |
| ||
metrics.firstResponseExecMicros.sum | NumberLong | ||
metrics.firstResponseExecMicros.max | NumberLong | ||
metrics.firstResponseExecMicros.min | NumberLong | ||
metrics.firstResponseExecMicros.sumOfSquares | NumberDecimal |
| |
metrics.docsReturned | |||
metrics.docsReturned.sum | NumberLong | ||
metrics.docsReturned.max | NumberLong | ||
metrics.docsReturned.min | NumberLong | ||
metrics.docsReturned.sumOfSquares | NumberDecimal |
| |
metrics.firstSeenTimestamp | Date | ||
metrics.latestSeenTimestamp | Date | ||
metrics.workingTimeMillis | NumberLong | ||
metrics.delinquentAcquisitions | NumberLong |
| |
metrics.totalAcquisitionDelinquencyMillis | NumberLong |
| |
metrics.maxAcquisitionDelinquencyMillis | NumberLong |
| |
metrics.cpuNanos |
| ||
metrics.cpuNanos.sum | NumberLong |
| |
metrics.cpuNanos.max | NumberLong |
| |
metrics.cpuNanos.min | NumberLong |
| |
metrics.cpuNanos.sumOfSquares | NumberDecimal |
|
Collection Type集合类型
The key.collectionType field indicates the type of collection that the recorded query was issued on. The collectionType can be one of the following values:key.collectionType字段指示发出记录查询的集合类型。collectionType可以是以下值之一:
changeStream | |
collection | |
nonExistent | |
timeseries | |
view | |
virtual |
|
Query Shape查询形状
The key.queryShape document contains query shape fields. To learn about query shapes, see Query Shapes.key.queryShape文档包含查询形状字段。要了解查询形状,请参阅查询形状。
The fields in key.queryShape vary based on the command that resulted in the query stats entry. $queryStats creates query stats entries for aggregate, find, distinct, and count commands.key.queryShape中的字段会根据导致查询统计条目的命令而变化。$queryStats为aggregate、find、distinct和count命令创建查询统计条目。
Each query shape property corresponds to a query option. For example, 每个查询形状属性都对应一个查询选项。例如,key.queryShape.sort corresponds to the sort() specification for the query shape.key.queryShape.sort对应于查询形状的sort()规范。
find Command Query Shape查找命令查询形状
The following table describes the query shape properties for 下表描述了find commands.find命令的查询形状属性。
key.queryShape.filter | ||
key.queryShape.sort | ||
key.queryShape.projection | ||
key.queryShape.skip | ||
key.queryShape.limit | ||
key.queryShape.singleBatch | ||
key.queryShape.max | ||
key.queryShape.min | ||
key.queryShape.returnKey | ||
key.queryShape.showRecordId | ||
key.queryShape.tailable | ||
key.queryShape.oplogReplay | ||
key.queryShape.awaitData | ||
key.queryShape.collation | ||
key.queryShape.allowDiskUse | ||
key.queryShape.let |
aggregate Command Query Shape聚合命令查询形状
The following table describes the query shape properties for 下表描述了aggregate commands.aggregate命令的查询形状属性。
key.queryShape.pipeline | ||
key.queryShape.explain | ||
key.queryShape.allowDiskUse | ||
key.queryShape.collation | ||
key.queryShape.hint | ||
key.queryShape.let |
distinct Command Query Shape不同的命令查询形状
The following table describes the query shape properties for 下表描述了distinct commands.distinct命令的查询形状属性。
key.queryShape.collation | ||
key.queryShape.key | ||
key.queryShape.query |
count Command Query Shapecount命令查询形状
The following table describes the query shape properties for 下表描述了count commands.count命令的查询形状属性。
key.queryShape.collation | ||
key.queryShape.query | ||
key.queryShape.limit | ||
key.queryShape.skip |
Supplemental Metrics补充指标
Query stats entries may contain a 查询统计条目可能包含一个metrics.supplementalMetrics document that provides additional information about your queries.metrics.supplementalMetrics文档,提供有关查询的其他信息。
$vectorSearch Metrics$vectorSearch指标
If your query shape contains 如果您的查询形状包含$vectorSearch, $queryStats outputs the following supplemental metrics:$vectorSearch,$queryStats将输出以下补充指标:
metrics.supplementalMetrics.vectorSearch | $vectorSearch aggregation stage$vectorSearch聚合阶段的补充指标 | |
metrics.supplementalMetrics.vectorSearch.limit | limit value of the $vectorSearch aggregation stage$vectorSearch聚合阶段的limit值相关的度量 | |
metrics.supplementalMetrics.vectorSearch.numCandidatesLimitRatio | numCandidates value of the $vectorSearch aggregation stage. $vectorSearch聚合阶段的numCandidates值相关的度量。$queryStats expresses these metrics as a ratio of numCandidates divided by the limit value.$queryStats将这些指标表示为numCandidates除以limit值的比率。 |
Examples示例
MongoDB Shell
To run the examples in this section, start with the following data:要运行本节中的示例,请从以下数据开始:
db.products.insertMany(
[
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps" , qty: 30 }
]
)
Then, run these commands:然后,运行以下命令:
db.products.find( { item: "card" } )
db.products.aggregate( [
{
$match: { qty: { $gt: 20 } }
}
] )
The following examples show the output of 以下示例显示了使用不同类型的数据转换的$queryStats using different types of data transformation:$queryStats的输出:
The example 以下部分中的示例$queryStats output in the following sections may vary based on the execution of other commands.$queryStats输出可能因其他命令的执行而异。
Untransformed Example未转换示例
Input:输入:
db.getSiblingDB("admin").aggregate( [
{
$queryStats: { }
}
] )
Output:输出:
[
{
key: {
queryShape: {
cmdNs: { db: 'test', coll: 'products' },
command: 'find',
filter: { item: { '$eq': '?string' } }
},
client: {
driver: { name: 'nodejs|mongosh', version: '5.1.0' },
os: {
type: 'Darwin',
name: 'darwin',
architecture: 'arm64',
version: '22.6.0'
},
platform: 'Node.js v16.19.1, LE (unified)',
version: '5.1.0|1.8.0',
application: { name: 'mongosh 1.8.0' }
},
collectionType: 'collection'
},
keyHash: 'dsoJ+LHAru0z6MJ1/IygJnnLTrlpVYYmPnlmNZbZrLI=',
queryShapeHash: "uxMLCvpiJ5N/IRqt4c28/0A8F01C8AA16CA805FF5C1A5737535F97E40C2A90CE91A82CCB7A74C7CCB9C48",
metrics: {
lastExecutionMicros: Long("4254"),
execCount: Long("1"),
totalExecMicros: {
sum: Long("4254"),
max: Long("4254"),
min: Long("4254"),
sumOfSquares: Decimal128("18096516")
},
firstResponseExecMicros: {
sum: Long("4254"),
max: Long("4254"),
min: Long("4254"),
sumOfSquares: Decimal128("18096516")
},
docsReturned: {
sum: Long("1"),
max: Long("1"),
min: Long("1"),
sumOfSquares: Decimal128("1")
},
firstSeenTimestamp: ISODate("2023-09-14T12:30:27.989Z"),
latestSeenTimestamp: ISODate("2023-09-14T12:30:27.989Z")
},
asOf: Timestamp({ t: 1694695007, i: 0 })
},
{
key: {
queryShape: {
cmdNs: { db: 'test', coll: 'products' },
command: 'aggregate',
pipeline: [
{ '$match': { qty: { '$gt': '?number' } } }
]
},
apiVersion: '1',
client: {
driver: { name: 'nodejs|mongosh', version: '5.1.0' },
os: {
type: 'Darwin',
name: 'darwin',
architecture: 'arm64',
version: '22.6.0'
},
platform: 'Node.js v16.19.1, LE (unified)',
version: '5.1.0|1.8.0',
application: { name: 'mongosh 1.8.0' }
},
collectionType: 'collection',
cursor: { batchSize: '?number' }
},
keyHash: '2QLBfL0m1lliStdN4XvBjqVBtZQ6ffaB2L1pJ99twT8=',
queryShapeHash: "uxMLCvpiJ5N/IRqt4c28/0A8F01C8AA16CA805FF5C1A5737535F97E40C2A90CE91A82CCB7A74C7CCB9C48",
metrics: {
lastExecutionMicros: Long("350"),
execCount: Long("3"),
totalExecMicros: {
sum: Long("3084"),
max: Long("2499"),
min: Long("235"),
sumOfSquares: Decimal128("6422726")
},
firstResponseExecMicros: {
sum: Long("3084"),
max: Long("2499"),
min: Long("235"),
sumOfSquares: Decimal128("6422726")
},
docsReturned: {
sum: Long("3"),
max: Long("1"),
min: Long("1"),
sumOfSquares: Decimal128("3")
},
firstSeenTimestamp: ISODate("2023-11-29T21:16:17.796Z"),
latestSeenTimestamp: ISODate("2023-11-29T21:17:12.385Z")
},
asOf: Timestamp({ t: 1701292827, i: 0 })
}
]Transformed Example转换示例
Input:输入:
db.getSiblingDB("admin").aggregate( [
{
$queryStats: {
transformIdentifiers: {
algorithm: "hmac-sha-256" ,
hmacKey: BinData(8, "87c4082f169d3fef0eef34dc8e23458cbb457c3sf3n2")
}
}
}
] )
Output:输出:
[
{
key: {
queryShape: {
cmdNs: {
db: 'Mtrt3iG7dsX5c5uCSIhSVlcu5qD3u3xx2EQnS1dJLxM=',
coll: '3oJE6AyOuf8h5NqWiXETxulFlPm3QUXbMnMjL2EqAU4='
},
command: 'find',
filter: {
'VWVRow7Ure92ajRPfrpWiU8OtDeWcLePFIq0+tooBng=': { '$eq': '?string' }
}
},
client: {
driver: { name: 'nodejs|mongosh', version: '5.1.0' },
os: {
type: 'Darwin',
name: 'darwin',
architecture: 'arm64',
version: '22.6.0'
},
platform: 'Node.js v16.19.1, LE (unified)',
version: '5.1.0|1.8.0',
application: { name: 'mongosh 1.8.0' }
},
collectionType: 'collection'
},
keyHash: 'q4vxam+wbk8tTrl8D0MDFH1LQAbI8fWspfkGKhEUROk=',
queryShapeHash: "uxMLCvpiJ5N/IRqt4c28/0A8F01C8AA16CA805FF5C1A5737535F97E40C2A90CE91A82CCB7A74C7CCB9C48",
metrics: {
lastExecutionMicros: Long("4254"),
execCount: Long("1"),
keysExamined: {
sum: Int("5"),
max: Long("5"),
min: Long("5"),
sumOfSquares: Decimal128("25")
},
docsExamined: {
sum: Long("1"),
max: Long("1"),
min: Long("1"),
sumOfSquares: Decimal128("1")
},
hasSortStage: {true: Long("0"), false: Long("1")},
usedDisk: {true: Long("0"), false: Long("1")},
fromMultiPlanner: {true: Long("0"), false: Long("1")},
fromPlanCache: {true: Long("1"), false: Long("0")},
totalExecMicros: {
sum: Long("4254"),
max: Long("4254"),
min: Long("4254"),
sumOfSquares: Decimal128("18096516")
},
firstResponseExecMicros: {
sum: Long("4254"),
max: Long("4254"),
min: Long("4254"),
sumOfSquares: Decimal128("18096516")
},
docsReturned: {
sum: Long("1"),
max: Long("1"),
min: Long("1"),
sumOfSquares: Decimal128("1")
},
firstSeenTimestamp: ISODate("2023-09-14T12:30:27.989Z"),
latestSeenTimestamp: ISODate("2023-09-14T12:30:27.989Z")
},
asOf: Timestamp({ t: 1694695712, i: 0 })
},
{
key: {
queryShape: {
cmdNs: {
db: 'Mtrt3iG7dsX5c5uCSIhSVlcu5qD3u3xx2EQnS1dJLxM=',
coll: '3oJE6AyOuf8h5NqWiXETxulFlPm3QUXbMnMjL2EqAU4='
},
command: 'aggregate',
pipeline: [
{
'$match': {
'RVqrwNEPotzdKnma/T7s4YcgNvpqO29BMDoni2N4IMI=': { '$gt': '?number' }
}
}
]
},
apiVersion: '1',
client: {
driver: { name: 'nodejs|mongosh', version: '5.1.0' },
os: {
type: 'Darwin',
name: 'darwin',
architecture: 'arm64',
version: '22.6.0'
},
platform: 'Node.js v16.19.1, LE (unified)',
version: '5.1.0|1.8.0',
application: { name: 'mongosh 1.8.0' }
},
collectionType: 'collection',
cursor: { batchSize: '?number' }
},
keyHash: 'HEhpQTYB+/wVoHLkOkMd+EC2jguQlMJ1N/vTE7+b8Js=',
queryShapeHash: "uxMLCvpiJ5N/IRqt4c28/0A8F01C8AA16CA805FF5C1A5737535F97E40C2A90CE91A82CCB7A74C7CCB9C48",
metrics: {
lastExecutionMicros: Long("350"),
execCount: Long("3"),
keysExamined: {
sum: Int("5"),
max: Long("5"),
min: Long("5"),
sumOfSquares: Decimal128("25")
},
docsExamined: {
sum: Long("1"),
max: Long("1"),
min: Long("1"),
sumOfSquares: Decimal128("1")
},
hasSortStage: {true: Long("0"), false: Long("1")},
usedDisk: {true: Long("0"), false: Long("1")},
fromMultiPlanner: {true: Long("0"), false: Long("1")},
fromPlanCache: {true: Long("1"), false: Long("0")},
totalExecMicros: {
sum: Long("3084"),
max: Long("2499"),
min: Long("235"),
sumOfSquares: Decimal128("6422726")
},
firstResponseExecMicros: {
sum: Long("3084"),
max: Long("2499"),
min: Long("235"),
sumOfSquares: Decimal128("6422726")
},
docsReturned: {
sum: Long("3"),
max: Long("1"),
min: Long("1"),
sumOfSquares: Decimal128("3")
},
firstSeenTimestamp: ISODate("2023-11-29T21:16:17.796Z"),
latestSeenTimestamp: ISODate("2023-11-29T21:17:12.385Z")
},
asOf: Timestamp({ t: 1701293302, i: 0 })
},
]Node.js
To use the MongoDB Node.js driver to add a 要使用MongoDB Node.js驱动程序将$queryStats stage to an aggregation pipeline, use the $queryStats operator in a pipeline object.$queryStats阶段添加到聚合管道中,请在管道对象中使用$queryStats运算符。
Untransformed Example未转换示例
The following example creates a pipeline stage that outputs untransformed runtime statistics about recorded queries. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,该阶段输出有关已记录查询的未转换运行时统计信息。然后,该示例运行聚合管道:
const pipeline = [{ $queryStats: {} }];
const adminDb = client.db("admin");
const cursor = adminDb.aggregate(pipeline);
return cursor;
Transformed Example转换示例
To return transformed query statistics, include the 要返回转换后的查询统计信息,请在管道阶段中包含transformIdentifiers field in the pipeline stage:transformIdentifiers字段:
const pipeline = [
{
$queryStats: {
transformIdentifiers: {
algorithm: "hmac-sha-256",
hmacKey: new Binary(Buffer.from("87c4082f169d3fef0eef34dc8e23458cbb457c3aabbccddeeff00112233445566778899", "hex"), 8)
}
}
}
];
const adminDb = client.db("admin");
const cursor = adminDb.aggregate(pipeline);
return cursor;MongoDB Atlas Data CollectionMongoDB Atlas数据集合
MongoDB Atlas periodically uses MongoDB Atlas会定期使用$queryStats to collect anonymized data about your queries, which helps improve MongoDB products. Your data may also be used to make feature suggestions based on usage. MongoDB retains the data it collects with $queryStats for four years.$queryStats集合有关查询的匿名数据,这有助于改进MongoDB产品。您的数据也可能用于根据使用情况提出功能建议。MongoDB将使用$queryStats集合的数据保留四年。
When Atlas runs 当Atlas在您的部署上运行$queryStats on your deployment, it uses a unique HMAC key per Atlas organization to transform your data and avoid collecting sensitive information.$queryStats时,它会为每个Atlas组织使用一个唯一的HMAC键来转换您的数据并避免集合敏感信息。