Navigation
This version of the documentation is archived and no longer supported.

collMod

On this page

Definition

collMod

New in version 2.2.

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

Powers of Two Record Allocation

usePowerOf2Sizes

Changed in version 2.6: usePowerOf2Sizes became the default allocation strategy for all new collections. Set newCollectionsUsePowerOf2Sizes to false to select the exact fit allocation strategy for new collections.

The usePowerOf2Sizes flag changes the method that MongoDB uses to allocate space on disk for documents in this collection. By setting usePowerOf2Sizes, you ensure that MongoDB will allocate space for documents in sizes that are powers of 2 (e.g. 32, 64, 128, 256, 512…16777216.) The smallest allocation for a document is 32 bytes.

With usePowerOf2Sizes, MongoDB will be able to more effectively reuse space.

With usePowerOf2Sizes, MongoDB allocates records that have power of 2 sizes until record sizes equal 4 megabytes. For records larger than 4 megabytes with usePowerOf2Sizes set, mongod will allocate records in full megabytes by rounding up to the nearest megabyte.

Use usePowerOf2Sizes for collections where applications insert and delete large numbers of documents to avoid storage fragmentation and ensure that MongoDB will effectively use space on disk.

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.

Examples

Enable Powers of Two Allocation

To enable usePowerOf2Sizes on the collection named products, use the following operation:

db.runCommand( {collMod: "products", usePowerOf2Sizes : true })

To disable usePowerOf2Sizes on the collection products, use the following operation:

db.runCommand( { collMod: "products", usePowerOf2Sizes: false })

usePowerOf2Sizes only affects subsequent allocations caused by document insertion or record relocation as a result of document growth, and does not affect existing allocations.

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 }
←   compact reIndex  →