# MongoDB Atlas Serverless Instances: Quick Start

MongoDB Atlas serverless instances are now GA (generally available)!

What is a serverless instance you might ask? In short, *it’s an on-demand serverless database*. In this article, we'll deploy a MongoDB Atlas serverless instance and perform some basic CRUD operations. You’ll need a MongoDB Atlas account. If you already have one sign-in, or register now.

## Demand Planning

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.

## Ongoing Management

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.

## The Solve

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.

## Create Your First Serverless Instance

Let’s see how it works…

If you haven’t already signed up for a MongoDB Atlas account, go ahead and do that first, then select "Build a Database".

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!

Your serverless instance will be up and running in just a few minutes. Alternatively, you can also use the Atlas CLI to create and deploy a new serverless instance.

While we wait for that, let’s set up a quick Node.js application to test out the CRUD operations.

## Node.js CRUD Example

Prerequisite: You will need Node.js installed on your computer.

Connecting to the serverless instance is just as easy as a tiered instance.

1.  Click “Connect.”

    

3.  Set your IP address and database user the same as you would a tiered instance.
4.  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`

### Create

We’ll create a `server.js` file in the root and paste the code example we just copied.

```js
const  MongoClient  =  require('mongodb').MongoClient;
const  uri  =  "mongodb+srv://mongo:@serverlessinstance0.xsel4.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const  client  =  new  MongoClient(uri, { useNewUrlParser:  true, useUnifiedTopology:  true });

client.connect(err  => {
    const  collection  =  client.db("test").collection("devices");
    
    // perform actions on the collection object
    
    client.close();
});
```

We’ll need to replace `` with our actual user password and `myFirstDatabase` with the database name we’ll be connecting to.

Let’s modify the `client.connect` method to create a database, collection, and insert a new document.

Now we’ll run this from our terminal using `node server`.

```js
client.connect((err) => {
    const  collection  =  client.db("store").collection("products");
    collection
        .insertOne(
        {
            name:  "JavaScript T-Shirt",
            category:  "T-Shirts",
        })
        .then(() => {
            client.close();
        });
});
```

When we use the `.db` and `.collection` methods, if the database and/or collection does not exist, it will be created. We also have to move the `client.close` method into a `.then()` after the `.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 `.insertMany()`.

```js
collection
    .insertMany(
    {
        name:  "React T-Shirt",
        category:  "T-Shirts",
    },
    {
        name:  "Vue T-Shirt",
        category:  "T-Shirts",
    }
    ])
    .then(() => {
        client.close();
    });
```

Make the changes and run `node server` again.

### Read

Let’s see what’s in the database now. There should be three documents. The `find()` method will return all documents in the collection.

```js
client.connect((err) => {
    const  collection  =  client.db("store").collection("products");
    collection.find().toArray((err, result) =>  console.log(result))
    .then(() => {
        client.close();
    });
});
```

When you run `node server` now, 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.

```js
client.connect((err) => {
    const  collection  =  client.db("store").collection("products");
    collection.find({name: “React T-Shirt”}).toArray((err, result) =>  console.log(result))
    .then(() => {
        client.close();
    });
});
```

### Update

To update a document, we can use the `updateOne()` method, passing it an object with the search parameters and information to update.

```js
client.connect((err) => {
    const  collection  =  client.db("store").collection("products");
    collection.updateOne(
        { name:  "Awesome React T-Shirt" },
        { $set: { name:  "React T-Shirt" } }
    )
    .then(() => {
        client.close();
    });
});
```

To see these changes, run a `find()` or `findOne()` again.

### Delete

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.

```js
client.connect((err) => {
    const  collection  =  client.db("store").collection("products");
    collection.deleteOne({ name:  "Vue T-Shirt" }).then(() =>  client.close());
});
```

## Conclusion

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.

I’d love to hear your feedback or questions. Let’s chat in the [MongoDB Community.