- Reference >
- Database Commands >
- Administration Commands >
- collMod
collMod¶
On this page
Definition¶
-
collMod¶ collModmakes it possible to add options to a collection or to modify view definitions. The command takes the following prototype form:For the
<collection or view>, specify the name of a collection or view in the current database.Use the
userFlagsfield in thedb.collection.stats()output to check the options enabled for a collection.
Options¶
TTL Collections¶
-
index¶ The
indexoption changes the expiration time of a TTL Collection.Specify the key or index name, and new expiration time with a document of the form:
In this example,
<index_spec>is an existing index in the collection. In cases of multiple indexes with the same key pattern, the user is required to specify the index by name.secondsis the number of seconds to subtract from the current time.On success
collModreturns a document with fieldsexpireAfterSeconds_oldandexpireAfterSeconds_newset to their respective values.On failure,
collModreturns a document withno expireAfterSeconds field to updateif there is no existingexpireAfterSecondsfield orcannot find index { **key**: 1.0 } for ns **namespace**if the specifiedkeyPatterndoes not exist.
Record Allocation¶
-
noPadding¶ New in version 3.0.
noPaddingis available for the MMAPv1 storage engine only.noPaddingdisables record padding for the collection. Without padding, the record allocation size corresponds to the document size, and any updates that results in document growth will require a new allocation.Warning
Only set
noPaddingtotruefor collections whose workloads have no update operations that cause documents to grow, such as for collections with workloads that are insert-only. For more information, see No Padding Allocation Strategy.
-
usePowerOf2Sizes¶ Deprecated since version 3.0.
usePowerOf2Sizesis available for the MMAPv1 storage engine only.usePowerOf2Sizeshas no effect on the allocation strategy. MongoDB 3.0 uses the power of 2 allocation as the default record allocation strategy for MMAPv1.To disable the power of 2 allocation for a collection, use the
collModcommand with thenoPaddingoption or thedb.createCollection()method with thenoPaddingoption. For more information, see MMAPv1 Record Allocation Behavior Changes.
Document Validation¶
-
validator¶ New in version 3.2.
validatorallows users to specify validation rules or expressions for a collection. For more information, see Document Validation.The
validatoroption takes a document that specifies the validation rules or expressions. You can specify the expressions using the same operators as the query operators with the exception of :$geoNear,$near,$nearSphere,$text, and$where.Note
- Validation occurs during updates and inserts. Existing documents do not undergo validation checks until modification.
- You cannot specify a validator for collections in the
admin,local, andconfigdatabases. - You cannot specify a validator for
system.*collections.
-
validationLevel¶ New in version 3.2.
The
validationLeveldetermines how strictly MongoDB applies the validation rules to existing documents during an update.validationLevelDescription "off"No validation for inserts or updates. "strict"Default Apply validation rules to all inserts and all updates. "moderate"Apply validation rules to inserts and to updates on existing valid documents. Do not apply rules to updates on existing invalid documents.
-
validationAction¶ New in version 3.2.
The
validationActionoption determines whether toerroron invalid documents or justwarnabout the violations but allow invalid documents.Important
Validation of documents only applies to those documents as determined by the
validationLevel.validationActionDescription "error"Default Documents must pass validation before the write occurs. Otherwise, the write operation fails. "warn"Documents do not have to pass validation. If the document fails validation, the write operation logs the validation failure.
To view the validation specifications for a collection, use the
db.getCollectionInfos() method.
Views¶
-
viewOn¶ The underlying source collection or view for the view. The view definition is determined by applying the specified
pipelineto this source.Required if modifying a view on a MongoDB deployment that is running with access control.
-
pipeline¶ The aggregation pipeline that defines the view.
Required if modifying a view on a MongoDB deployment that is running with access control.
The view definition is public; i.e.
db.getCollectionInfos()andexplainoperations on the view will include the pipeline that defines the view. As such, avoid referring directly to sensitive fields and values in view definitions.
Access Control¶
If the deployment enforces authentication/authorization, you must have
the following privilege to run the collMod command:
| Required Privileges | |
|---|---|
| Modify a non-capped collection | collMod in the database |
| Modify a view |
|
The built-in role dbAdmin provides the required privileges.
Examples¶
Change Expiration Value for Indexes¶
To update the expiration value for a collection
named sessions indexed on a lastAccess field from 30
minutes to 60 minutes, use the following operation:
Which will return the document:
Add Document Validation to an Existing Collection¶
The following example adds a validator to a collection named contacts.
The validator specifies that inserted or updated documents should meet at least
one of the following conditions:
- the
phonefield is a string - the
emailfield matches the regular expression - the
statusfield is eitherUnknownorIncomplete.
The moderate validationLevel specifies that only updates
to existing valid documents will be checked against the validator, and
the warn validationAction means that the write operation
will log a validation failure for any document that does not pass
validation.
With the validator in place, the following insert operation fails validation:
The write operation logs the failure and succeeds:
For more information, see Document Validation.