EventGet 50% off your ticket to MongoDB.local London on October 2. Use code WEB50Learn more >>
MongoDB Developer
JavaScript
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
JavaScriptchevron-right

Getting Started With Bun and MongoDB

Joel Lord9 min read • Published Jul 19, 2024 • Updated Jul 19, 2024
Node.jsTypeScriptJavaScript
Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
JavaScript has come a long way since its creation in the 1990s. It's now much more than a front-end programming language used to create fancy effects on a webpage. It has matured and is now an enterprise-grade back-end language with Node.js.
In the last few years, some alternatives for using JavaScript on the back end have emerged. In addition to Node.js, runtimes such as Deno aim to fix some of the issues engineers have with JavaScript.
This article will focus on Bun, a new generation runtime for running JavaScript or TypeScript on a server.
Bun is a new runtime designed for speed. It is optimized for large-scale web servers and includes modern tooling such as a built-in TypeScript interpreter, test runner, and package manager.
It is fully compatible with Node.js, and the packages you use with your other projects should work with Bun just as well.
In this quickstart, we'll introduce you to using Bun to write a simple CRUD API to read and write from a MongoDB collection. This simple guide uses the built-in packages from Bun to create a web server and the MongoDB Node.js driver to connect to the database.

Prerequisites

  • Bun: Install Bun by following the instructions on their website.
  • MongoDB: Use your own MongoDB instance or create a free-forever Atlas cluster
  • (Optional) Sample dataset: If you want to start with some data, you can import the sample dataset on your free Atlas cluster

TLDR

If you want to start with a MongoDB and Bun template, you can run the following commands.

Set-up

Like in a typical Node.js project, you will need to start by initializing your application. This will create the necessary scaffolding for your application.
We will keep our project very simple for this tutorial and use the built-in Bun.serve web server. The only dependency required is the mongodb package from npm.
In a new folder, run the following commands to initialize your project and install the required dependencies.
If you look at the files in your folder, you should now see an index.ts file, along with the dependencies installed in the node_modules folder. If you are a Node.js developer, this should seem very familiar.

Application structure

Our application will be a simple API with five basic routes.
  • POST /movies: Will take a movie in the body and insert it in the collection
  • GET /movies: Retrieves the latest 10 movies that were added to the database
  • GET /movies/:id: Returns a single movie by id
  • PUT /movies/:id: Updates the movie specified in the path with the body
  • DELETE /movies/:id: Deletes the movie specified in the path
The routes will all be handled in the main index.ts file. The logic to connect to the database is located in the utils/db.ts file, and all the actions that are performed on the database can be found in the contollers/movies.ts file.

Starting the application

Let's start with a basic web server. Bun has multiple built-in packages, including Bun.serve. This package contains all the necessary components to build a basic web server.
Replace the content of the index.ts file with the following code:
Now, start the application with the following command.
Using --watch will automatically reload the server whenever a file changes. This is very convenient while you're in development mode.
By default, Bun will use port 3000 with Bun.serve. You can change that by setting your shell's PORT environment variable. Bun will automatically use the port defined in that environment variable.
To try it out, use Postman or a similar service, your web browser, or a CLI tool such as curl. For this article, I'll use curl.
Run the following command in a new terminal window to test your application.
You should see a message saying Welcome to the movie database.
Congrats! You have successfully built a web server with Bun --- time to connect to a MongoDB database.

Connecting to MongoDB

The connection to the MongoDB database will be handled by a file called utils/db.ts. Here, you will use the MongoDB driver (already installed with bun add) to create a MongoClient. You will then connect to the movies collection and export that collection for your controllers to use.
In a production environment, you'd also add some logic to ensure that the database connection is working properly, but for the purpose of this article, we'll stick to the basics.
Start with a new file called utils/db.ts.
You will notice that the connection string to MongoDB (MONGODB_URI) is read directly from the environment variables. With Bun, no need to use a package to inject those variables into your application. It will automatically read from your .env file.
So, let's go ahead and create a .env file in your project's root folder.
Replace the value of the environment variable with your connection string.
Great work! You can now connect to the database. The code isn't invoked anywhere, so there isn't much to test. Let's see how we can use this collection to perform CRUD operations.

Building a movie model

Bun uses TypeScript out of the box. You could also decide to use plain JavaScript, but since we're leveraging TS for this application, we'll need to create a movie model to tell our application what a movie looks like. For this quickstart, our movies will only have a title, some actors, and the year in which they were released.
Create a models/movies.ts file:
Note how we're also using the native MongoDB ObjectId type here for the _id field.
Because MongoDB is a database, your documents can contain many different properties, including arrays of strings, as we do in this example. We're keeping things simple here, but it could even be more complex objects, such as an array of actor objects. You can find out more about the document model on MongoDB University.
Now that you have your model, you can go ahead and create your movies controller.

Create a movies controller

You have a client connected to your database and a model that manages the format of your data. It's now time to do those CRUD operations on your database.
These operations are all handled in the movies controller. This controller communicates with the database and returns the operation's result to our server.
Create a new file called controllers/movies.ts
This controller lists the most basic operations that can be performed on the database. If you want to learn more about those operations, I recommend the MongoDB and Node.js Tutorial - CRUD Operations on Developer Center. You can also look at our docs for the find, findOne, insertOne, updateOne, and deleteOne methods from our driver in our docs.
If you need more advanced operations, such as grouping or faceting, you should also look at the MongoDB Aggregation framework.
Now that you have all the code in place to perform your CRUD operations on your collection, it's time to return to the server and start with the request routing.

Server routing

It's now time to tie everything together in our index.ts file which is the code for the server itself. In this file, we'll import the necessary components, perform some routing, and call the appropriate methods from our controller.
First, import the necessary files at the top of the index.ts file.
This will import the movie controller we created earlier and the necessary types we'll use.
Next up, rewrite your Bun.serve logic to support various routes.
We need to implement a full routing system because we don't use any framework here. In a production environment, you would most likely use a framework such as Hono or Express to help you with this.
In this case, we write a regular expression matching any route that starts with /movies. We also look at the method sent in the request. If the route doesn't match one of the handle routes or starts with /movies, we return the message 404!.
You can test these routes using curl.
Now's the time to connect your server to your database.

Connecting everything

We can finally put everything together.

Add a movie

To add a movie to the database, you must read the body of the request using req.json() and send this movie to your controller.
Here, we're assuming that the request always matches a movie object, but in reality, you'd want to validate this. You would also likely want to handle any errors when inserting the data.
Once the code is in place, you can add a new movie to the collection.
You should see an acknowledgement that the operation worked and receive the new insertedId.

Read movies

Our GET /movies route is slightly more complex as it will handle both /movies to retrieve a list of 10 movies and /movies/:id to retrieve a single movie. To figure out which one to use, we'll look for the existence of a parameter after the /movies component.
If it's there, we'll take this string and convert it to an ObjectId before sending it to the movies controller. If the parameter isn't there, we use the getMovies method from the movies controller to return the latest 10.
You can now test those new routes. Note that you'll need to change the route of the second curl command to match an ObjectId from your existing movie collection.

Update movies

We use the same trick to update a movie to find the id parameter and convert it to an ObjectId. We then pass the body of the request to the update method from our movies controller.

Delete movies

Finally, to delete a movie, we extract the movie id from the pathname and pass it to our movie controller's delete function.

What's next?

That's it! You have a fully functional server running on Bun that can connect to your MongoDB collection and perform basic CRUD operations. You can find all the code in our GitHub repository.
To deploy this code into production, you'd want to add more robustness to your code. You would also need to handle errors and return the appropriate error codes when a document is not found. But this is a good starting point for your first application.
If you have any questions, please register to our community forums, and use the form below to contact us!
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Using MongoDB Atlas Triggers to Summarize Airbnb Reviews with OpenAI


Oct 31, 2023 | 4 min read
Tutorial

How to Build an Animated Timeline Chart with the MongoDB Charts Embedding SDK


Dec 13, 2023 | 6 min read
Article

How to Enable Local and Automatic Testing of Atlas Search-Based Features


Jun 12, 2024 | 8 min read
Tutorial

IoT and MongoDB: Powering Time Series Analysis of Household Power Consumption


Aug 28, 2024 | 6 min read
Table of Contents