Modifying collection behavior using the collMod command

This is a command that will be executed to change the behavior of a collection in Mongo. It can be thought of as a collection-modifying operation (it is not mentioned anywhere officially though).

For a part of this recipe, knowing about TTL indexes is required.

Getting ready

In this recipe, we will be executing the collMod operation on a collection. We need to start a standalone server listening to any port for client connections; in this case, we will stick to the default 27017. If you are not aware of how to start a standalone server, refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server. We also need to start a shell that will be used for this administration. It is highly recommended you take a look at the Expiring documents after a fixed interval using the TTL index and Expiring documents at a given time using the TTL index recipes in Chapter 2, Command-line Operations and Indexes, if you are not aware of them.

How to do it…

The collMod operation can be used to do a few things:

  1. Let us change the space allocation on the disk for the new document being added. A collection needs to exist to execute the collMod command. You can try to execute this command against any existing collection. In our case, I am assuming we have a collection in place called powerOfTwoCol, which was created using the following command from the Mongo shell:
    > db.createCollection('powerOfTwoCol')
    
  2. Once the collection is in place/created, execute the following command:
    > db.runCommand({collMod: 'powerOfTwoCol', usePowerOf2Sizes: 1})
    
  3. Let us now change the settings of the TTL index. Assuming we have a collection with a TTL index, as we saw in Chapter 2, Command-line Operations and Indexes, we can do that by executing the following command:
    > db.ttlTest.getIndexes()
    
  4. To change the expiry time to 800 ms from 300 ms, execute the following command:
    > db.runCommand({collMod: 'ttlTest', index: {keyPattern: {createDate:1}, expireAfterSeconds:800}})
    

How it works…

The collMod command always has the {collMod : <name of the collection>, <collmod operation>} format. There are two possible operations currently supported that we will see. We will break our explanation into two parts.

First, we will see what happens by setting usePowerOf2Sizes. If a collection is heavy on updates and the documents grow in size, it will be moved on the disk when it can no longer grow where it is placed. This causes a hole to be left on the disk space for the collection at the place where the document originally was. Mongo uses these holes to accommodate new documents wherever possible. However, by using the usePowerOf2Sizes setting, Mongo allocates disk space in numbers by the power of two (32, 64, 128, 256, …), with the minimum value being 32. This setting does use a few more extra spaces as compared to a normal document without this setting, as the disk space used is rounded always by the power of two. However, in the long term, when the documents get updated frequently and grow in size, the disk usage is better, so is the performance, as document movement is reduced. Thus, if you foresee this pattern of documents growing in size with time, setting this option might be a good idea. However, for patterns where documents are just inserted and rarely updated, we are better off with the default settings (till version 2.4). Also, if the collection already has data when this option is set, the subsequent allocation for the new documents would be by the power of two, without affecting the existing documents.

From Version 2.6 of MongoDB, the usePowerOf2Sizes strategy is the default option for all collections and thus, usePowerOf2Sizes:false is the only sensible option to use in the collMod operation. When starting the server, a new server startup parameter newCollectionUsePowerOf2Sizes is available and defaults to the value true. This option can be used to disable the usePowerOf2Sizes setting by providing the value false to it. Setting this value to false will ensure that the size allocated to a new document will use the strategy that is followed till version 2.4 by default, which provides space that is needed by the document times the padding factor.

The second operation by using collMod is to change the TTL index. If a TTL index has already been created and the time to live needs to be changed after creation, we use the collMod command. This operation-specific field is as follows:

{index: {keyPattern: <the field on which the index was originally created>, expireAfterSeconds:<new time to be used for TTL of the index>}}

The keyPattern is the field on which the TTL index is created, and expireAfterSeconds will contain the new time to be changed to. On successful execution, we should see the following output in the shell:

{ "expireAfterSeconds_old" : 300, "expireAfterSeconds_new" : 800, "ok" : 1 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset