Working with a Collection in MongoDB

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This is beginner’s guide to building collections within MongoDB. We will show you basic commands to use when working with collections. Interesting fact: MongoDB will accommodate large documents of up to 16 MB in collections. While we are not going to be building a large collection today, it’s good to know there are limitations and to remember to keep your individual document size small. Let’s get started with working with a collection in MongoDB.

Visualizing MongoDB

Starting in MongoDB can be challenging, especially if you are coming from a SQL world. Before we get into working with our collection, let’s visualize MongoDB from a relational database frame of mind.

The concept of a Database is the same for both RDBMS and MongoDB. Both are containers for data that need to be written or retrieved. When we talk about collections, it’s similar to what a table is in RDBMS. From there we know that collections contain documents; documents are like rows in RDBMS. In each document you have a set of fields; fields are like columns in RDBMS.

  • MongoDB ———> RDBMS
  • Database ——–> Database
  • Collections —–> Tables
  • Documents ——-> Rows
  • Fields ———-> Columns

So creating a collection is just a container for all your data. Let’s get started!

Prerequisites

  • You should have MongoDB installed
  • It’s not required but it’s recommended that you have some previous command line experience with MongoDB.
  • You should have access to the MongoDB command line ( Execute mongo )

Create a Collection

Before we jump into collections, we must create a database, then we can add a collection. Let’s setup our database:

1
2
3
4
5
6
> use authors
switched to db authors
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Right now, this new database won’t show up on the list because it doesn’t have any data. There are two ways to create a new collection. The first is by creating a collection as you insert a document into the database using an insert() statement. This will create a collection instantly when one doesn’t exist:

1
2
> db.authors.insert({"name" : "Mark Twain", "genre" : "fiction", "title" : "The Adventures of Huckleberry Finn"})
WriteResult({ "nInserted" : 1 })

An alternative way is to use a createCollection() statement without any data to create a blank collection (we will name this collection books:

1
2
> db.createCollection("books")
{ "ok" : 1 }

Capped Collections

Before we move deeper into collections, it’s good to note that there are options when creating a collection. You can create a “capped collection” that will be a fixed-size collection if you need to allocatate a specific amount of space. In this scenario, documents will be added until that space fills up. After that it will start to overwrite the documents in the collection, starting with the oldest.

The benefits of a capped collection is that it reduces overhead when working with high-throughput insertions.

If you plan on using a capped collection you can set your options using db.createCollection (name, options). An example would be:

1
db.createCollection("logs", { capped: true, size: 500000, max: 200 });

Where the size is the maximum size in bytes for the collection and the max is the number of documents allowed in the collection.

Insert Data Into a Collection

Depending on how many documents you want to create, you have different options to insert data. You have already become familiar with the insert() method to add a single document into a collection. However, this method can also add a bulk array of documents at once:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> db.authors.insert(
... [
...   {name: "Anne Rice"},
...   {name: "William Shakespeare"},
...   {name: "Steven King"},
...   {name: "J.K. Rowling"},
... ]
... )
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 4,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]

The result shows that we bulk inserted four documents successfully into the collection.

Other options to insert are insertOne() and insertMany() which you will likely be using more often. As their method names suggest, once is to add a single document while the other is to add multiple documents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
> db.books.insertOne({title: "Jane Eyre", genre: "fiction"})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5d676c3ccaacec22eec01cf8")
}



> db.books.insertMany(
... [
...   {title: "To Kill a Mockingbird", genre: "fiction"},
...   {title: "The Shining", genre: "horror"},
...   {title: "The Princess Bride", genre: "romance"},
... ]
... )
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5d676d14caacec22eec01cf9"),
        ObjectId("5d676d14caacec22eec01cfa"),
        ObjectId("5d676d14caacec22eec01cfb")
    ]
}

It is important to know that even though insert() does both single and multiple documents, this is typically only used when writing in the mongo shell. In fact, this method is deprecated in the major driver. So be sure and use the insertOne() and insertMany() when writing your applications.

Querying a Collection

To display collection records, simply use the find() method to run a query to retrieve the document in a collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.books.find()
{ "_id" : ObjectId("5d676c3ccaacec22eec01cf8"), "title" : "Jane Eyre", "genre" : "fiction" }
{ "_id" : ObjectId("5d676d14caacec22eec01cf9"), "title" : "To Kill a Mockingbird", "genre" : "fiction" }
{ "_id" : ObjectId("5d676d14caacec22eec01cfa"), "title" : "The Shining", "genre" : "horror" }
{ "_id" : ObjectId("5d676d14caacec22eec01cfb"), "title" : "The Princess Bride", "genre" : "romance" }


> db.authors.find()
{ "_id" : ObjectId("5d670d46caacec22eec01cf3"), "name" : "Mark Twain", "genre" : "fiction", "title" : "The Aventures of Huckleberry Finn" }
{ "_id" : ObjectId("5d676779caacec22eec01cf4"), "name" : "Anne Rice" }
{ "_id" : ObjectId("5d676779caacec22eec01cf5"), "name" : "William Shakespeare" }
{ "_id" : ObjectId("5d676779caacec22eec01cf6"), "name" : "Steven King" }
{ "_id" : ObjectId("5d676779caacec22eec01cf7"), "name" : "J.K. Rowling" }

This result is acceptable if you have documents with a few fields, but when your documents have more fields it’s difficult to read on one line. Use the pretty() method with your query to display a readable output of the document’s contents:

1
2
3
4
5
6
7
> db.authors.find({name: "Mark Twain"}).pretty()
{
    "_id" : ObjectId("5d670d46caacec22eec01cf3"),
    "name" : "Mark Twain",
    "genre" : "fiction",
    "title" : "The Aventures of Huckleberry Finn"
}

Deleting a Collection

To delete a collection that is no longer needed, use the drop() method on any named collection. In this example, there is a collection that was accidentally created that needs to be deleted:

1
2
3
4
5
6
7
8
9
10
11
12
> show collections
authors
books
mycollection

> db.mycollection.drop()
true


> show collections
authors
books

If the collection exists, and is deleted successfully, the response will return true. If the collection is non-existent, then the response will return false as in the example below:

1
2
3
4
5
6
> show collections
authors
books

> db.random.drop()
false

Conclusion

In this article you learned how to create and use collections, including querying for collection records. In addition, you gained some insight into capped collections and how they can be help you maintain performance with high-throughput insertions. Finally, you learned about different insertion methods and when to use them.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.