Try Studio 3T for Free
  1. Knowledge Base
  2. MongoDB Tutorials
  3. Working with MongoDB Collections: CRUD, Count, Sort, Limit Collection Size & More

Working with MongoDB Collections: CRUD, Count, Sort, Limit Collection Size & More

collection in MongoDB is a group of documents.

Collections in a NoSQL database like MongoDB correspond to tables in relational database management systems (RDBMS) or SQL databases.  As such, collections don’t enforce a set schema, and documents within a single collection can have widely different fields.

In this guide, we’ll show you how to perform CRUD operations in the mongo shell and through the GUI, Studio 3T, as well as other time-saving shortcuts like copy-pasting collections across databases, viewing multiple collections at once, and more.

Download the latest version of Studio 3T here.

Create a MongoDB collection – db.createCollection()

In the mongo shell, creating a collection is done through the db.createCollection() method.

A collection can be created with or without set parameters, which include size (maximum size of a document in bytes).

Without parameters:


With parameters, in this case “max” (maximum number of documents):

         max: 300,

In Studio 3T, you can create a collection by right-clicking on the target database’s Collections folder from the Connection Tree and choosing Add Collection.

Right-click on a Collections folder to add a collection

This will open a wizard where you can configure collation, validation, storage engine options, maximum number of documents, and more.

Add collection wizard

Drop a MongoDB collection – drop()

Removing a collection from the database is done through the drop() method.  Leaving the field blank deletes the entire collection, like so:


In Studio 3T, the same can be done by right-clicking on the collection and choosing Drop Collection or pressing Del (Fn + Del).

Drop a MongoDB collection

Rename a MongoDB collection – renameCollection()

Use the renameCollection() method in the mongo shell, specifying the target name in brackets:


Or right-click on the target collection and choose Rename Collection in Studio 3T:

Rename MongoDB collection

View a collection’s size – totalSize()

To determine a collection’s size, use the totalSize() method:


It returns the collection’s total storage size, plus the total size of each of the collection’s indexes in bytes.

View a collection’s document count – count()

To count the number of documents in a collection, or the number of documents a find query would return, use the count() method.

Total document count:


Count for documents matching manatee:

             pet: "manatee",

View a collection’s stats – stats()

To view a collection’s overall stats (including size and document count), use the stats() method. Size data by default is returned in bytes but can be modified with the scale value, in this case kilobytes at by specifying 1024:

             scale: 1024,

In Studio 3T, you can view all these collection statistics at a glance by right-clicking on the target collection and selecting Collection Statistics, which will display the information in tree view.

MongoDB collection statistics at a glance

View multiple MongoDB collections side-by-side

Very frequently, you probably encounter one of these use cases:

  1. You want to easily compare test and production data sets.
  2. You simply want to look at and quickly compare result sets of different queries.

Let’s focus on the second use case and look at two separate views of the same collection.

This will result in two different tabs displayed one after the other like this:

Before: Two MongoDB collections opened in two tabs, with no way to view both at once

With the Split Vertically / Split Horizontally feature, Studio 3T allows you to quickly see, in a single glance, multiple tabs side by side. You can choose to display the two datasets side by side vertically or horizontally.

Split vertically

Start by right clicking on any of the tabs and choose Split Vertically.

Right-click on a tab and choose either Split Vertically or Split Horizontallly

Now your two tabs will be shown side-by-side like this:

After: Two vertically split tabs

Split tabs horizontally

Or you could choose to split your view horizontally. Again, start by right clicking one of your tabs and now choose Split Horizontally.

After: Two tabs split horizontally

Drag and drop tabs

You can now even drag-and-drop tabs and reorder them in case you have multiple tabs open and you want a quick way to compare them more easily.

Open multiple tabs at once, even drag and drop them as you need

Sort MongoDB collections

While in Studio 3T’s Table View, tap on a column header once to sort your view based on the data in that column in ascending order.

If you click on the header again, the view will be sorted in descending order. Another tap will restore the original (unsorted) order.

Limit MongoDB collection size

By default, Studio 3T loads the first 50 documents of a collection (i.e. dbCursor.find({}).limit(50)) when first opening a collection view.

As MongoDB documents can be up to 16MB in size, this can amount to serious traffic, so we’ve introduced a convenient way of limiting this initial number of documents either for all or specific collections.

Limit number of documents loaded for all collections

  1. Go to Studio 3T > Preferences.
  2. By default, you’ll land on the General tab.
  3. The first field you’ll see is Initial page size when showing result documents. Change the number as needed.
    This will be the initial page size for all collections.
    You can change the initial page size for all collections under Studio 3T > Preferences
  4. Click OK.

Limit number of documents loaded for specific collections

You can also limit the initial number of documents loaded for particular collections, which could come in handy when opening collections with exceptionally large documents, for example.

    1. Right-click on the collection you’d like to open in the Open Connections Tree.
    2. Choose the second option: Open Collection with Custom Page Size.Define the page size of any MongoDB collection you open
    3. Enter the number of documents or initial page size you’d like to load initially.
    4. Click OK.

Once you’ve defined your initial page size, explore your collections in Tree, JSON or Table View. Or for better data analysis, compare your MongoDB collections and spot differences in documents and fields.

Complement this read with our guide on Performing MongoDB CRUD Operations on Documents.

Updated on April 29, 2020

Was this article helpful?