MongoDB / MongoDB Basics
Using the MongoDB Shell
This tutorial will introduce you to the MongoDB Shell, an interactive JavaScript interface for MongoDB. You'll learn how to perform queries, update data, and carry out administrat…
Section overview
5 resourcesCovers the fundamental concepts of MongoDB, including collections, documents, and CRUD operations.
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.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article