Definition
New in version 8.0.
Deletes query settings previously added with
setQuerySettings.
To delete query settings, you must provide either a query shape hash string or a query shape to removeQuerySettings.
To find a hash string or query shape, you can use a
$querySettings stage in an aggregation pipeline. The hash
string is named queryShapeHash in the $querySettings output.
If you provide a query shape to removeQuerySettings, include the
fields for the existing query settings shape to delete. The field values
don't have to match. For example, if you have existing query settings
for find x=1 and provide find x=100 to removeQuerySettings,
removeQuerySettings deletes the query settings for find x=1.
For more information about query shapes, see Query Shapes.
Syntax
You can delete query settings using either of the following syntax specifications.
Provide a Query Shape Hash String
In the following syntax, you provide a query shape hash string
in removeQuerySettings:
db.adminCommand( { removeQuerySettings: <string> // Provide an existing query shape hash string } )
Provide a Query Shape
In the following syntax, you provide:
The same fields as a
find,distinct, oraggregatecommand. See the syntax sections on the pages for those commands for the fields you can include inremoveQuerySettings.A
$dbfield that specifies the database name associated with the original command.
db.adminCommand( { removeQuerySettings: { <fields>, // Provide fields for // find, distinct, or aggregate command $db: <string> // Provide a database name } } )
Command Fields
The command takes this field:
Examples
The following examples create a collection, add query settings, and delete the settings:
Create the example collection and index
Run:
// Create pizzaOrders collection 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 db.pizzaOrders.createIndex( { orderDate: 1 } )
The index has the default name orderDate_1.
Add the query settings
The following setQuerySettings example adds query settings:
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" } } )
Return the query settings
The following example uses $querySettings to return the
query settings:
db.aggregate( [ { $querySettings: {} } ] )
To locate the query settings to delete, use the queryShapeHash
string in this output:
[ { queryShapeHash: 'F42757F1AEB68B4C5A6DE6182B29B01947C829C926BCC01226BDA4DDE799766C', 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('2024-01-20T00:00:00.000Z') } }, sort: { totalNumber: 1 }, '$db': 'test' } } ]
Delete the query settings
The following example uses removeQuerySettings to delete the
query settings identified using queryShapeHash from
the previous output:
db.adminCommand( { removeQuerySettings: "F42757F1AEB68B4C5A6DE6182B29B01947C829C926BCC01226BDA4DDE799766C" } )
You can also delete query settings using a query shape. For example:
db.adminCommand( { removeQuerySettings: { find: "pizzaOrders", filter: { orderDate: { $gt: ISODate( "2023-01-20T00:00:00Z" ) } }, sort: { totalNumber: 1 }, $db: "test" } } )