Using the Mongo Shell: Using Collections

In the following subsections we shall create a collection and subsequently drop a collection from the Mongo shell.

1. Creating a Collection

The create command is used to create a collection. We already discussed the create command to create a collection (see “Running a Command or Method in mongo Shell”). The show collections command may be used to list the collections in a database.

>use test >show collections

The collections in the database get listed as shown in Figure 2-14.

The collections listed could be different for different users but the system.indexes collection should get listed regardless of the user. A collection gets listed only after it has been created. For example, list the collections before and after running the db.createCollection() method to create a collection catalog in the test database. If the catalog collection has already been created, drop the collection with db.catalog.drop()

>use test


>show collections >db.createCollection(“catalog”)

>show collections

The show collections command runs after the db.createCollection() method lists the catalog collection as shown in Figure 2-15. The other collections listed could be different for different users.

A capped collection is a collection with a fixed size, similar to a circular list. When a capped collection is full the data gets overwritten. Data cannot be deleted from a capped collection. For example, create a capped collection catalog with auto indexing and 64 KB size with maximum number of documents as 1000. Before creating the capped catalog collection, drop the collection if created previously using the db.catalog.drop() command. (Dropping is discussed in more detail in the following section.)

>use test


>db.createCollection(“catalog”, {capped: true, autoIndexId: true, size: 64 * 1024, max: 1000} )

A response with ok field as 1 indicates the capped collection gets created as shown in Figure 2-16.

The create command may also be run using the db.runCommand() method. As before, if the catalog collection already exists drop it first. The use test command is not required to be rerun if already using the test database.

>use test


>db.runCommand( { create: “catalog”, capped: true, size: 64 * 1024, max: 1000 } )

The capped collection gets created as indicated by the “ok”: 1 response in Figure 2-17.

A collection must not already exist or an error gets generated. For example, create the catalog collection when it already exists. The “collection already exists” error message gets output as shown in Figure 2-18.

The number of documents in a collection may be listed with the following helper method db.collection.count(). For example, the following method lists the document count in the catalog collection.


An output of 0 indicates no documents in the catalog collection have been added, as shown in Figure 2-19.

The stats on a collection may be listed with the following db.collection.stats() method. For example, the following method lists the stats on the catalog collection.


The collection stats output include the collection namespace in the format <database>.<collection>, the document count, if the collection is capped, the storage size, and the maximum number of documents and more as shown in Figure 2-20.

A collection may be renamed with the db.collection.renameCollection(target, dropTarget) method. By default the dropTarget boolean is false indicating if a collection by the same name as the new name of the collection already exists should the target collection be dropped. For example, rename the mongo collection to mongodb.

>db.mongo.renameCollection(‘mongodb’, false)

A response of “ok”: 1 indicates the mongo collection gets renamed as shown in Figure 2-21. If you run the show collections command after the renaming, it lists the renamed collection mongodb instead of the mongo collection listed prior to renaming.

2. Dropping a Collection

To remove a collection from the database invoke the db.collection.drop() method. For example, invoke the show collections command to list all collections. Subsequently drop the catalog collection.

>show collections


>show collections

If the show collections command is invoked subsequent to dropping the catalog collection, the catalog collection is not listed as shown in Figure 2-22.

In most examples in this chapter we have dropped a previously created collection and created the same collection for the example to demonstrate the effect of the command used in the example. Running commands with a previously created collection may not fully demonstrate the effect of a command.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *