Connect to Mongo (Atlas)
In this tutorial, you will learn how to integrate a MongoDB Atlas database in a genezio project.
Prerequisites
If you don't already have them, you'll need to install the following tools:
You need to have a genezio project. Use an existing one, or create a new one.
1. Initialize a MongoDB Atlas database
For detailed explanations about this step, click here.
If you already have a MongoDB, skip to step 2.
2. Integrate your newly created cluster into the project
Create a .env
file and add a line with the MONGO_DB_URI=<your_connection_string>
value.
A step-by-step guide on how to get MONGO_DB_URI
can be found here.
You can then use it all over your code using process.env.MONGO_DB_URI
.
3. Create a new DB connection into a class
Now that we have the connection string, you can integrate MongoDB into your classes.
We will use mongoose
it to access the database.
npm install mongoose
Now create a file mongoDbService.ts
import { GenezioDeploy } from "@genezio/types";
import mongoose from "mongoose";
@GenezioDeploy()
export class TutorialClass {
constructor() {
this.#connect();
}
/**
* Private method used to connect to the DB.
*/
#connect() {
mongoose.connect(process.env.MONGO_DB_URI || "").catch((error) => {
console.log("Error connecting to the DB", error);
});
}
async addUser(name: string) {
const dbConnection = mongoose.connection;
// Access the collection directly
const collection = dbConnection.collection("users");
// Create the collection if it doesn't exist
await collection.createIndex({ name: 1 });
// Insert an object into the collection
const response = await collection.insertOne({ name: name });
return response;
}
}
Into the class’s constructor, you need to establish the connection with the database.
Now you can use all the functions provided by mongoose
all over your class.
Insights
Database connection error
You might encounter an issue when connecting to your database. One of the main reasons is that your IP might not be allowed to access the cluster. To change this, go to your cluster configuration, and on the tab ‘Network Access’ add your IP or 0.0.0.0
to give full IP access to the database.
Serverless database access
With MongoDB Atlas, there are 2 main ways to query the database.
Persistent Connection
This is the most conventional method of establishing a connection to a database, but [IT] encounters certain issues in a serverless environment, particularly when the initial connection consumes a significant amount of time.
Data API
Mongo DB Atlas Data API offers a solution where you can directly access the database through an API, eliminating the requirement of establishing an initial connection and reducing serverless cold-start significantly.
Next Steps
Other things that do not depend on connecting to a database are scheduling the execution of a function as a cron job, or implementing HTTP Webhooks:
Also, you can find more details on deploying the backend and frontend here:
Now you are ready for some more advanced use cases:
Support
We invite you to join our community on Discord for further information and help.
Happy Learning!