Setup and Basic Field Selection
In this first step, you will connect to the MongoDB server, create a database and a collection, and insert some sample data. Then, you will perform your first data transformation by selecting and renaming specific fields from the documents.
First, open your terminal and launch the MongoDB Shell (mongosh). This interactive shell is the primary way to interact with your MongoDB instance. You will perform all database operations within this shell for the remainder of the lab.
mongosh
Once you are inside the MongoDB Shell, your prompt will change. Now, create and switch to a new database named bookstore. If the database does not exist, this command will create it.
use bookstore
Next, create a collection named books and insert three sample documents into it using the insertMany command. A collection is a group of documents, similar to a table in a SQL database.
db.books.insertMany([
{
title: "MongoDB Basics",
author: "Jane Smith",
price: 29.99,
pages: 250,
categories: ["Database", "Programming"]
},
{
title: "Python Deep Dive",
author: "John Doe",
price: 39.99,
pages: 450,
categories: ["Programming", "Python"]
},
{
title: "Data Science Handbook",
author: "Alice Johnson",
price: 49.99,
pages: 600,
categories: ["Data Science", "Programming"]
}
]);
Now that you have data, let's use the aggregation pipeline to transform it. The aggregate method takes an array of stages, where each stage performs an operation on the data. Our first stage will be $project, which reshapes the documents.
Run the following command to select only the title and author fields, renaming them to bookTitle and bookAuthor respectively.
db.books.aggregate([
{
$project: {
_id: 0,
bookTitle: "$title",
bookAuthor: "$author"
}
}
]);
You should see the following output:
[
{ "bookTitle": "MongoDB Basics", "bookAuthor": "Jane Smith" },
{ "bookTitle": "Python Deep Dive", "bookAuthor": "John Doe" },
{ "bookTitle": "Data Science Handbook", "bookAuthor": "Alice Johnson" }
]
Let's break down the $project stage:
_id: 0 excludes the default _id field from the output. By default, it is always included.
bookTitle: "$title" creates a new field named bookTitle and assigns it the value of the original title field. The $ prefix indicates that you are referencing the value of a field.
bookAuthor: "$author" similarly renames the author field to bookAuthor.