- Reference >
- Database Commands >
- Administration Commands >
- collMod
collMod¶
On this page
Definition¶
- collMod¶
collMod makes it possible to add flags to a collection to modify the behavior of MongoDB. Flags include usePowerOf2Sizes and index. The command takes the following prototype form:
db.runCommand( {"collMod" : <collection> , "<flag>" : <value> } )
In this command substitute <collection> with the name of a collection in the current database, and <flag> and <value> with the flag and value you want to set.
Use the userFlags field in the db.collection.stats() output to check enabled collection flags.
Flags¶
TTL Collection Expiration Time¶
- index¶
The index flag changes the expiration time of a TTL Collection.
Specify the key and new expiration time with a document of the form:
{keyPattern: <index_spec>, expireAfterSeconds: <seconds> }
In this example, <index_spec> is an existing index in the collection and seconds is the number of seconds to subtract from the current time.
On success collMod returns a document with fields expireAfterSeconds_old and expireAfterSeconds_new set to their respective values.
On failure, collMod returns a document with no expireAfterSeconds field to update if there is no existing expireAfterSeconds field or cannot find index { **key**: 1.0 } for ns **namespace** if the specified keyPattern does not exist.
Record Allocation¶
Disable All Record Padding¶
- noPadding¶
3.0 新版功能.
noPadding flag is available for the MMAPv1 storage engine only.
noPadding disables 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.
警告
Only set noPadding to true for 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.
Powers of Two Record Allocation¶
- usePowerOf2Sizes¶
3.0 版后已移除.
usePowerOf2Sizes flag is available for the MMAPv1 storage engine only.
usePowerOf2Sizes has 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 collMod command with the noPadding flag or the db.createCollection() method with the noPadding option. For more information, see MMAPv1 Record Allocation Behavior Changes.
Document Validation¶
- validator¶
3.2 新版功能.
validator allows users to specify validation rules or expressions for a collection. For more information, see Document Validation.
The validator option 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.
注解
- 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, and config databases.
- You cannot specify a validator for system.* collections.
- validationLevel¶
3.2 新版功能.
The validationLevel determines how strictly MongoDB applies the validation rules to existing documents during an update.
validationLevel Description "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¶
3.2 新版功能.
The validationAction option determines whether to error on invalid documents or just warn about the violations but allow invalid documents.
重要
Validation of documents only applies to those documents as determined by the validationLevel.
validationAction Description "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.
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:
db.runCommand( { collMod: "sessions",
index: { keyPattern: { lastAccess: 1 },
expireAfterSeconds: 3600
}
})
Which will return the document:
{ "expireAfterSeconds_old" : 1800, "expireAfterSeconds_new" : 3600, "ok" : 1 }
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 phone field is a string
- the email field matches the regular expression
- the status field is either Unknown or Incomplete.
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.
db.runCommand( { collMod: "contacts",
validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
},
validationLevel: "moderate",
validationAction: "warn"
} )
With the validator in place, the following insert operation fails validation:
db.contacts.insert( { name: "Amanda", status: "Updated" } )
The write operation logs the failure and succeeds:
2015-10-15T11:20:44.260-0400 W STORAGE [conn3] Document would fail validation collection: example.contacts doc: { _id: ObjectId('561fc44c067a5d85b96274e4'), name: "Amanda", status: "Updated" }
For more information, see Document Validation.