Definition定义
$querySettings
New in version 8.0.在版本8.0中新增。
Returns query settings previously added with 返回以前使用setQuerySettings. The settings apply to the entire cluster. The cluster retains the settings after shutdown.setQuerySettings添加的查询设置。这些设置适用于整个集群。集群在关闭后保留设置。
Syntax语法
If you use multiple pipeline stages, put 如果使用多个管道阶段,请将$querySettings first in the pipeline.$querySettings放在管道的第一位。
db.aggregate( [
{ $querySettings: { showDebugQueryShape: <boolean> } },
// Add any additional pipeline stages after $querySettings在$querySettings之后添加任何其他管道阶段
...
] )Command Fields命令字段
$querySettings takes this field:接受此字段:
showDebugQueryShape |
|
Examples示例
MongoDB Shell
The following examples create a collection, add query settings, and return the settings:以下示例创建集合、添加查询设置并返回设置:
Create the example collection and indexes创建示例集合和索引
Run:运行:
// Create pizzaOrders collection创建pizzaorders集合
db.pizzaOrders.insertMany( [
{ _id: 0, type: "pepperoni", totalNumber: 5,
orderDate: new Date( "2024-01-15T12:00:00Z" ) },
{ _id: 1, type: "cheese", totalNumber: 15,
orderDate: new Date( "2024-01-23T11:12:32Z" ) },
{ _id: 2, type: "vegan", totalNumber: 20,
orderDate: new Date( "2024-03-20T10:01:12Z" ) }
] )
// Create ascending index on orderDate field在orderDate字段上创建升序索引
db.pizzaOrders.createIndex( { orderDate: 1 } )
// Create ascending index on totalNumber field在totalNumber字段上创建升序索引
db.pizzaOrders.createIndex( { totalNumber: 1 } )
The indexes have the default names 索引的默认名称为orderDate_1 and totalNumber_1.orderDate_1和totalNumber_1。
Add the query settings添加查询设置
The following 以下setQuerySettings example adds query settings:setQuerySettings示例添加了查询设置:
db.adminCommand( {
setQuerySettings: {
find: "pizzaOrders",
filter: {
orderDate: { $gt: ISODate( "2024-01-20T00:00:00Z" ) }
},
sort: {
totalNumber: 1
},
$db: "test"
},
settings: {
indexHints: {
ns: { db: "test", coll: "pizzaOrders" },
allowedIndexes: [ "orderDate_1" ]
},
queryFramework: "classic",
comment: "Index hint for orderDate_1 index to improve query performance"
}
} )
The comment field is available starting in MongoDB 8.1 (and 8.0.4).comment字段从MongoDB 8.1(和8.0.4)开始可用。
Return the query settings返回查询设置
The following example uses a 以下示例使用聚合管道中的$querySettings stage in an aggregation pipeline to return query settings:$querySettings阶段返回查询设置:
db.aggregate( [ {
$querySettings: { showDebugQueryShape: true }
} ] )
Because 因为showDebugQueryShape is true, the debugQueryShape document is included in the output. You can use the queryShapeHash identifier to locate the query settings. showDebugQueryShape为true,所以debugQueryShape文档包含在输出中。您可以使用queryShapeHash标识符来定位查询设置。queryShapeHash and debugQueryShape are highlighted in this output:queryShapeHash和debugQueryShape在此输出中突出显示:
[
{
queryShapeHash: 'AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1',
settings: {
indexHints: [
{
ns: { db: 'test', coll: 'pizzaOrders' },
allowedIndexes: [ 'orderDate_1' ]
}
],
queryFramework: 'classic',
comment: 'Index hint for orderDate_1 index to improve query performance'
},
representativeQuery: {
find: 'pizzaOrders',
filter: { orderDate: { '$gt': ISODate('2023-01-20T00:00:00.000Z') } },
sort: { totalNumber: 1 },
'$db': 'test'
},
debugQueryShape: {
cmdNs: { db: 'test', coll: 'pizzaOrders' },
command: 'find',
filter: { orderDate: { '$gt': '?date' } },
sort: { totalNumber: 1 }
}
}
]Node.js
The Node.js examples on this page use the 本页上的Node.js示例使用Atlas示例数据集中的sample_mflix database from the Atlas sample datasets. sample_mflix数据库。To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB Node.js driver documentation.要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门。
To use the MongoDB Node.js driver to add a 要使用MongoDB Node.js驱动程序将$querySettings stage to an aggregation pipeline, use the $querySettings operator in a pipeline object.$querySettings阶段添加到聚合管道中,请在管道对象中使用$querySettings运算符。
To use the 要使用$querySettings stage, you must first add indexes and querySettings to the collection.$querySettings阶段,您必须首先将索引和querySettings添加到集合中。
The following code inserts two indexes on the 以下代码在movies collection:movies集合上插入两个索引:
const year = collection.createIndex({ year: 1 });
const title = collection.createIndex({ title: 1 });
The following code adds query settings to the 以下代码将查询设置添加到movies collection:movies集合中:
const command = {
setQuerySettings: {
find: "movies",
filter: { year: { $gt: 2011 } },
sort: { title: 1 },
$db: "sample_mflix"
},
settings: {
indexHints: {
ns: { db: "sample_mflix", coll: "movies" },
allowedIndexes: [ "year_1" ]
},
queryFramework: "classic",
comment: "Index hint for year_1 index to improve query performance"
}
};
const adminDb = client.db("admin");
const result = adminDb.command(command);
The following example creates a pipeline stage that returns the previously added query settings for the 以下示例创建了一个管道阶段,该阶段返回之前为movies collection. movies集合添加的查询设置。The example sets the 该示例将showDebugQueryShape option to true to include the debugQueryShape document in the output. The example then runs the aggregation pipeline:showDebugQueryShape选项设置为true,以在输出中包含debugQueryShape文档。然后,该示例运行聚合管道:
const pipeline = [ { $querySettings: {showDebugQueryShape: true} } ];
const cursor = adminDb.aggregate(pipeline);
return cursor;