Documentation Index
Fetch the complete documentation index at: https://mongodb-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Create a MongoDB Atlas Deployment
This section shows how to set up a local or cloud MongoDB Atlas deployment and connect to the deployment by using the MongoDB Shell.
Install dependencies
Before you begin this tutorial, you must install the following dependencies in your development environment:
- Atlas CLI: Command-line interface that allows you to manage your deployments from the terminal
- MongoDB Shell: Interactive tool that connects to a deployment and provides database operation support
- Docker: Platform that allows you to run software within containers, including local MongoDB deployments
Select the tab corresponding to your operating system to view the commands that install these required development tools.Run the following commands to install the dependencies by using the Homebrew package manager. If you do not have Homebrew, you can install it by following the instructions on the Homebrew website.brew install mongodb-atlas
brew install --cask docker
Run the following commands to install the dependencies by using the Chocolatey package manager. If you do not have Chocolatey, you can install it by following the instructions on the Chocolatey website.choco install mongodb-atlas
choco install docker-desktop
For other ways to install the Atlas CLI, see Atlas CLI install page.The preceding commands install the Docker Desktop application. After the installation completes, ensure that you create a Docker account and start the application. Set up a fully-featured deployment
Run the following command and follow the prompts in the shell to deploy a cluster. If you do not have an Atlas account, the following command prompts you to create one. Local Deployment
Cloud Deployment
This command creates a single-member replica set in a container that runs on your local machine.atlas deployments setup myDeployment --type local \
--mdbVersion 8.0 --port <port number> --connectWith skip
Replace the <port number> placeholder with the port you want to use. The default port is 27017, but you can specify a different port if 27017 is not available.
The command outputs the following information:Deployment created!
Connection string: "<connection string URI>"
Save the connection string URI for use in a future step. This command creates a free tier cluster on MongoDB Atlas.atlas deployments setup myDeployment --type atlas \
--provider AWS -r us-east-1 --skipSampleData \
--username <database username> --password <database user password> \
--connectWith skip --force
Replace the following placeholder values to create a new database user with atlasAdmin privileges in your deployment:
<database username>: Specify a username for your new database user
<database user password>: Specify a password for your new database user
The command outputs the following information:Cluster created.
Your connection string: "<connection string URI>"
Save the connection string URI for use in a future step. Connect to your deployment
You can connect to your deployment with the MongoDB Shell (mongosh) by running the following command: Local Deployment
Cloud Deployment
atlas deployments connect myDeployment --connectWith mongosh
After connecting, you can run the following command to test your connection:atlas deployments connect myDeployment --username <database username> \--password <database user password> --connectWith mongosh
atlas deployments connect myDeployment --username <database username> \
--password <database user password> --connectWith mongosh
Replace the <database username> and <database user password> placeholders with the username and password you created for your database user.
After connecting, you can run the following command to test your connection:The command returns a list of databases in your deployment.
Congratulations! You have successfully set up your MongoDB Atlas deployment and connected to it. To learn more about how to interact with your deployment by using the MongoDB Shell, see the MongoDB Shell documentation.
In the next section, you will learn how to create an application that connects to your deployment and interacts with data.
Create Your First MongoDB Application
To connect to your MongoDB Atlas deployment in an application, you can use one of the official MongoDB client libraries.
Select your preferred programming language from the following dropdown menu to learn how to connect to your MongoDB Atlas deployment in that language.
Before running the steps in this section, ensure that you exit the MongoDB Shell by running the exit command.
Load sample data
You can run the following commands to load sample data into your deployment: Local Deployment
Cloud Deployment
Run the following commands from your terminal to install the MongoDB Database Tools:brew tap mongodb/brew
brew install mongodb-database-tools
Then, run the following commands to load the sample data:curl https://atlas-education.s3.amazonaws.com/sampledata.archive -o sampledata.archive
mongorestore --archive=sampledata.archive --port <port number>
Replace the <port number> placeholder with the port number of your deployment. Your port number can be found in Docker Desktop.
atlas clusters sampleData load myDeployment
Initialize your application
Run the following commands in your terminal to create a new directory and a file for your application. The command also initializes npm in the directory and installs the Node.js driver.mkdir node-quickstart
cd node-quickstart
touch index.js
npm init -y
npm install mongodb
mkdir node-quickstart
cd node-quickstart
type nul > index.js
npm init -y
npm install mongodb
Create your application
Copy and paste the following code into index.js. This code connects to your cluster and queries your sample data.const { MongoClient } = require("mongodb");
async function runGetStarted() {
// Replace the uri string with your connection string
const uri = "<connection string URI>";
const client = new MongoClient(uri);
try {
const database = client.db("sample_mflix");
const movies = database.collection("movies");
// Queries for a movie that has a title value of 'Back to the Future'
const query = { title: "Back to the Future" };
const movie = await movies.findOne(query);
console.log(movie);
} finally {
await client.close();
}
}
runGetStarted().catch(console.dir);
Add your connection string
In your application file, replace the <connection string URI> placeholder with your connection string. The connection string has the following format: Local Deployment
Cloud Deployment
mongodb://localhost:<port number>/?directConnection=true
Replace the <port number> placeholder with the port number of your local deployment. Your port number can be found in Docker Desktop.mongodb+srv://<database user>:<database password>@<cluster address>
Replace the following placeholder values:
<database user>: The database username you specified when creating your deployment.
<database password>: The password for the database user.
<cluster address>: The SRV address for your cluster. You can view your cluster address in the connection string you saved after creating your deployment, or by running atlas clusters connectionStrings describe myDeployment in your terminal and copying the text after the mongodb+srv:// prefix.
Run your application
In your project directory, run the following command to start the application:The application output contains details about the retrieved movie document:{
_id: ...,
plot: 'A young man is accidentally sent 30 years into the past...',
genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ],
...
title: 'Back to the Future',
...
}
Next Steps
To learn more about your MongoDB deployment, see the following resources:
To access free courses for beginners and more advanced MongoDB users, visit the MongoDB University.