MongoDB Atlas Serverless Instances: Quick Start
Rate this quickstart
MongoDB Atlas serverless instances are now GA (generally available)!
When you deploy a MongoDB Atlas cluster, you need to understand what compute and storage resources your application will require so that you pick the correct tier to accommodate its needs.
As your storage needs grow, you will need to adjust your cluster’s tier accordingly. You can also enable auto-scaling between a minimum and maximum tier.
Once you’ve set your tiering scale, what happens when your app explodes and gets tons of traffic and exceeds your estimated maximum tier? It’s going to be slow and unresponsive because there aren’t enough resources.
Or, maybe you’ve over-anticipated how much traffic your application would get but you’re not getting any traffic. You still have to pay for the resources even if they aren’t being utilized.
As your application scales, you are limited to these tiered increments but nothing in between.
These tiers tightly couple compute and storage with each other. You may not need 3TB of storage but you do need a lot of compute. So you’re forced into a tier that isn’t balanced to the needs of your application.
MongoDB Atlas serverless instances solve all of these issues:
- Deployment friction
- Management overhead
- Performance consequences
- Paying for unused resources
- Rigid data models
With MongoDB Atlas serverless instances, you will get seamless deployment and scaling, a reliable backend infrastructure, and an intuitive pricing model.
It’s even easier to deploy a serverless instance than it is to deploy a free cluster on MongoDB Atlas. All you have to do is choose a cloud provider and region. Once created, your serverless instance will seamlessly scale up and down as your application demand fluctuates.
The best part is you only pay for the compute and storage resources you use, leaving the operations to MongoDB’s best-in-class automation, including end-to-end security, continuous uptime, and regular backups.
Let’s see how it works…
Next, choose the Serverless deployment option.
Now, select a cloud provider and region, and then optionally modify your instance name. Create your new deployment and you’re ready to start using your serverless instance!
While we wait for that, let’s set up a quick Node.js application to test out the CRUD operations.
Connecting to the serverless instance is just as easy as a tiered instance.
- Click “Connect.”
- Set your IP address and database user the same as you would a tiered instance.
- Choose a connection method.
- You can choose between mongo shell, Compass, or “Connect your application” using MongoDB drivers.
We are going to “Connect your application” and choose Node.js as our driver. This will give us a connection string we can use in our Node.js application. Check the “Include full driver code example” box and copy the example to your clipboard.
To set up our application, open VS Code (or your editor of choice) in a blank folder. From the terminal, let’s initiate a project:
npm init -y
Now we’ll install MongoDB in our project:
npm i mongodb
We’ll create a
server.jsfile in the root and paste the code example we just copied.
We’ll need to replace
<password>with our actual user password and
myFirstDatabasewith the database name we’ll be connecting to.
Let’s modify the
client.connectmethod to create a database, collection, and insert a new document.
Now we’ll run this from our terminal using
When we use the
.collectionmethods, if the database and/or collection does not exist, it will be created. We also have to move the
client.closemethod into a
.insertOne()promise has been returned. Alternatively, we could wrap this in an async function.
We can also insert multiple documents at the same time using
Make the changes and run
Let’s see what’s in the database now. There should be three documents. The
find()method will return all documents in the collection.
When you run
node servernow, you should see all of the documents created in the console.
If we wanted to find a specific document, we could pass an object to the
find()method, giving it something to look for.
To update a document, we can use the
updateOne()method, passing it an object with the search parameters and information to update.
To see these changes, run a
To delete something from the database, we can use the
deleteOne()method. This is similar to
find(). We just need to pass it an object for it to find and delete.
It’s super easy to use MongoDB Atlas serverless instances! You will get seamless deployment and scaling, a reliable backend infrastructure, and an intuitive pricing model. We think that serverless instances are a great deployment option for new users on Atlas.