One of the most common operations you’ll perform when working with MongoDB is querying document data.
The better you understand how to retrieve data, the more effectively you can access the information you need – when you need it.
Querying data is part of a larger set of operations commonly referred to as CRUD (Create, Read, Update, and Delete).
- When you insert a document into a MongoDB database, you create the data.
- When you query a document in the database, you read the data.
- When you modify a document, you update the data
- When you remove a document, you delete the data.
Each exercise in this section is tied to a specific CRUD operation. This will allow you to see first-hand how querying documents fits into the larger data-manipulation picture.
As part of the exercises, you’ll create a database named sales
and then add the customers
collection to the database, importing the data from a .json
file. You’ll then carry out all four MongoDB CRUD operations against the collection, using features built into Studio 3T.
Performing MongoDB CRUD operations on the Collection Tab
Studio 3T provides a variety of tools for carrying out CRUD operations against MongoDB document data.
One of the most powerful is the Collection Tab, which includes numerous features for creating, reading, updating, and deleting data.
As soon as you open the Collection Tab, you have immediate access to the collection’s documents. You can view them in Tree View, Table View, or JSON View, and you can use the query form at the top of the Collection Tab to quickly retrieve specific documents.
You can also add, modify, and delete documents almost just as easily.
The Collection Tab
The Collection Tab is divided into two main sections.
- The top section contains the query form and provides access to Visual Query Builder, a robust point-and-click tool for building MongoDB queries.
- The bottom section is divided into three tabs: Result, Query Code, and Explain. You view document data on the Result tab, choosing from the multiple views.
For example, the following figure shows the customers collection tab, with the data displayed in Tree View. This is the collection you’ll be using for the exercises in this course.
You can carry out all CRUD operations within the Result tab.
If you want to change an individual value, you only need to double-click the value in the data grid and update it in-place.
If you want to make more updates, you can click the Edit Document button on the toolbar and update the JSON definition directly.
As you work through the exercises in this section, you’ll get a better sense of the many options available on the Collection Tab for performing CRUD operations. Just know that Studio 3T also provides other options for carrying out these operations, especially when it comes to querying data.
By the end of this section, you will learn how to:
- Add a document to a collection
- View a document in a collection
- Update a document in a collection
- Delete a document in a collection
What you will need:
- Access to a MongoDB Atlas cluster
- The ability to download a .json file from the Internet