Using the MongoDB Shell

Tutorial 4 of 5

1. Introduction

1.1 Tutorial's Goal

This tutorial will provide a hands-on introduction to the MongoDB Shell (mongo shell), an interactive JavaScript interface to MongoDB. You will learn how to perform basic data operations, queries, and administrative tasks directly from the command line.

1.2 Learning Outcomes

By the end of this tutorial, you will:

  • Be able to connect to a MongoDB instance using the MongoDB Shell
  • Understand basic CRUD operations (Create, Read, Update, Delete)
  • Learn how to perform simple and complex queries
  • Know how to carry out administrative tasks

1.3 Prerequisites

  • Basic knowledge of JavaScript
  • MongoDB installed on your machine

2. Step-by-Step Guide

2.1 Connecting to MongoDB

To begin, launch the MongoDB Shell:

mongo

This command will connect to a MongoDB instance running on localhost and default port 27017.

2.2 Databases and Collections

MongoDB organizes data into databases, and each database contains collections. Collections are similar to tables in relational databases. To see all databases:

show dbs

To switch to or create a database:

use myDatabase

To create a collection:

db.createCollection('myCollection')

2.3 CRUD Operations

CRUD stands for Create, Read, Update, and Delete. These are the four basic functions of persistent storage.

  • Create: To insert a document into a collection:
db.myCollection.insert({name: 'John', age: 25, email: 'john@example.com'})
  • Read: To read documents:
db.myCollection.find()
  • Update: To update a document:
db.myCollection.update({name: 'John'}, {$set: {email: 'john.doe@example.com'}})
  • Delete: To delete a document:
db.myCollection.remove({name: 'John'})

3. Code Examples

3.1 Inserting Multiple Documents

db.myCollection.insertMany([
  {name: 'Alice', age: 20, email: 'alice@example.com'},
  {name: 'Bob', age: 30, email: 'bob@example.com'}
])

This will insert two new documents into myCollection.

3.2 Querying with Conditions

db.myCollection.find({age: {$gt: 25}})

This will return all documents where age is greater than 25.

3.3 Updating Multiple Documents

db.myCollection.updateMany({age: {$lt: 25}}, {$set: {status: 'Under 25'}})

This will update all documents where age is less than 25, setting a new field status with value 'Under 25'.

4. Summary

In this tutorial, we walked you through a hands-on introduction to the MongoDB Shell. You learned how to connect to a MongoDB instance, create databases and collections, and perform CRUD operations. You also learned how to query with conditions and update multiple documents.

Next, you might want to explore MongoDB's advanced querying capabilities, indexing, and aggregation framework. Start with MongoDB's official documentation, which is an excellent resource.

5. Practice Exercises

5.1 Exercise 1

Create a new database exerciseDB, and inside this database, create a new collection exerciseCollection. Insert a document with the following fields: name, age, email.

5.2 Exercise 2

In exerciseCollection, find the document you just inserted.

5.3 Exercise 3

Update the email field of the document you inserted in Exercise 1.

5.4 Solutions

Solution to Exercise 1

use exerciseDB
db.createCollection('exerciseCollection')
db.exerciseCollection.insert({name: 'Test', age: 30, email: 'test@example.com'})

Solution to Exercise 2

db.exerciseCollection.find({name: 'Test'})

Solution to Exercise 3

db.exerciseCollection.update({name: 'Test'}, {$set: {email: 'test.updated@example.com'}})

Feel free to experiment with different queries and operations to get more comfortable with the MongoDB Shell.