Getting going
You probably have a preferred tool for prototyping aggregation pipelines, having already explored the MongoDB aggregation framework before reaching for this book. However, suppose you are looking for alternatives. In that case, in the following section, you will find suggestions to get a MongoDB database and client tool up and running, ready to execute the example aggregations presented in this book.
Setting up your environment
To develop aggregation pipelines effectively, and to try the examples in Part 2: Aggregations by Example, you will need:
- A MongoDB database, version 4.2 or greater, that is network accessible from your workstation
- A MongoDB client tool running on your workstation to submit aggregation pipeline execution requests and view the results
Note
In Part 2: Aggregations by Example, most example aggregation pipelines are compatible with MongoDB version 4.2 and above. However, some examples utilize aggregation features introduced after version 4.2. For these, the book specifies the minimum MongoDB version required.
Database
The MongoDB database deployment for you to connect to can be a single server, a replica set, or a sharded cluster. You can run this deployment locally on your workstation, remotely on-premises, or in the cloud. You will need the MongoDB URL to connect to the database and, if authentication is enabled, the credentials required for full read and write access.
If you don't have access to a MongoDB database, the two most accessible options for running a database are as follows:
- Provision a free-tier MongoDB cluster (see https://www.mongodb.com/docs/atlas/tutorial/deploy-free-tier-cluster/) in MongoDB Atlas, which is a MongoDB cloud-based database as a service (once it's deployed, in the Atlas console, there is a button you can click to copy the URL of the cluster)
- Install and run a single MongoDB server (see https://docs.mongodb.com/guides/server/install/) locally on your workstation
Note
Aggregation pipelines in Chapter 13, Full-Text Search Examples, use Atlas Search. Consequently, you must use Atlas for your database deployment if you want to run the few Atlas Search-based examples.
Client tool
There are various options for the client tool, some of which are:
- MongoDB Shell: Install the MongoDB command-line tool, MongoDB Shell, mongosh (see https://www.mongodb.com/try/download/shell)
- MongoDB for Visual Studio (VS) Code: Install MongoDB for VS Code (see https://www.mongodb.com/docs/mongodb-vscode/install/) and use the Playgrounds feature (see https://www.mongodb.com/docs/mongodb-vscode/playgrounds/)
- MongoDB Compass: Install the official MongoDB-provided graphical user interface (GUI) tool, MongoDB Compass (see https://www.mongodb.com/products/compass)
- Studio 3T: Install the third-party 3T Software Labs–provided GUI tool, Studio 3T (see https://studio3t.com/download/)
All examples in this book present code that is easy to copy and paste into MongoDB Shell, i.e., mongosh
, to execute. All subsequent instructions in this book assume you are using the shell. However, you will find it straightforward to use one of the mentioned GUI tools instead, to execute the code examples.
MongoDB Shell with Atlas database
Here is how you can connect MongoDB Shell to an Atlas free-tier MongoDB cluster:
mongosh "mongodb+srv://mycluster.a123b.mongodb.net/test" --username myuser
Before running the command, ensure:
- You add your workstation's IP address (see https://www.mongodb.com/docs/atlas/security/add-ip-address-to-list/) to the Atlas access list
- You create a database user (see https://www.mongodb.com/docs/atlas/government/tutorial/create-mongodb-user-for-cluster/) for the deployed Atlas cluster, with rights to create, read, and write to any database
- You change the dummy URL and username text, shown in the preceding example command, to match your real cluster's details (these details are accessible via the cluster's
Connect
button in the Atlas console)
MongoDB Shell with local database
Here is the command for starting MongoDB Shell and connecting it to a MongoDB single-server database if you've installed MongoDB locally on your workstation:
mongosh "mongodb://localhost:27017"
MongoDB for VS Code
By using the MongoDB Playground tool in VS Code, you can quickly prototype queries and aggregation pipelines and execute them against a MongoDB database with the results shown in an output tab. Figure 1.3 shows the Playground tool in action:
Figure 1.3: MongoDB Playground tool in Microsoft Visual Studio Code
MongoDB Compass GUI
MongoDB Compass provides an Aggregation Pipeline Builder tool to assist users in prototyping and debugging aggregation pipelines and exporting them to different programming languages. You can see the aggregation tool in MongoDB Compass in Figure 1.4:
Figure 1.4: MongoDB Compass
Studio 3T GUI
Studio 3T provides an Aggregation Editor tool to help you prototype and debug aggregation pipelines and translate them to different programming languages. You can see the aggregation tool in Studio 3T in Figure 1.5:
Figure 1.5: Studio 3T