setDefaultCollectionOptions

On this page

Sets the defaults that are used when a collection is created implicitly by a write command for the current database. All the options supported by the createCollection command can be modified using setDefaultCollectionOptions.

Syntax

db.runCommand({
setDefaultCollectionOptions: 1
value : <document>|<null>
})

Field

Type

Description

value

Document

Specifies the options to set. When set to null, collection options are reset to the defaults set for new databases.

Example

The following example sets default collection options for the current database:

db.runCommand({
setDefaultCollectionOptions: 1,
value: { rowstore: true, shardKey: { _id: 1 } },
});

To update only one option from the defaults while leaving others unchanged, you may need to run multiple commands. Here's an example:

var options = db.runCommand({ getDefaultCollectionOptions: 1 }).value;
options.shardKey = { Code: 1 };
db.runCommand({ setDefaultCollectionOptions: 1, value: options });

Last modified: May 15, 2023

Was this article helpful?