Build an app / Node.js + MongoDB Driver
How to connect a Node.js app to SingleStore Kai using the MongoDB driver
This guide walks you through the steps to connect a Node.js application to a SingleStore database using the MongoDB driver. You’ll start by setting up a SingleStore deployment and workspace, then create a database and retrieve your connection credentials. Finally, you’ll configure your Node.js project by installing the mongodb package, loading environment variables, and establishing a connection to handle queries against your SingleStore instance. By the end, you’ll have a working example that you can use in your application.

Don’t have a SingleStore account yet?
Create deployment
1. Log in to your SingleStore Portal account.
2. In the left-hand menu, click Create New → Deployment.
3. In the Create Workspace form, follow the on‑screen instructions to complete the form.
4. Enable SingleStore Kai.
5. Click Create Workspace.
6. Wait for the workspace to finish deploying.
Create workspace
Note: If the required workspace already exists in the target deployment, you can skip this step.
1. Log in to your SingleStore Portal account.
2. In the left-hand menu, click Deployments.
3. From the deployments list, select the deployment where you want to create a workspace.
4. In the left‑hand pane, click + Create Workspace.
5. Enable SingleStore Kai.
6. Follow the on‑screen instructions to complete the form.
7. Click Create Workspace.
8. Wait for the workspace to finish deploying.
Create database
1. Log in to your SingleStore Portal account.
2. In the left-hand menu, click Deployments.
3. From the deployments list, select the deployment where you want to create a database.
4. In the right‑hand pane, click + Create Database.
5. In the Create Database form, enter a new database name and select the workspace to attach it to.
6. Click Create Database.
Retrieve database credentials
1. Log in to your SingleStore Portal account.
2. In the left-hand menu, click Deployments.
3. From the deployments list, select the deployment that contains your database.
4. From the workspaces list, select the workspace to which your database is attached.
5. In the selected workspace, click Connect.
6. In the Connect dropdown, choose MongoDB Client.
7. In the MongoDB Client tab, copy the connection string.
If you don’t know the password, click Reset Password, then copy the new password.
1. Create a .env file in the root of your project.
2. In this file, define your connection string and database name by adding DB_URL and DB_NAME variables, for example:
1
DB_URL="mongodb://<USER>:<PASSWORD>@<HOST>:<PORT>/?authMechanism=PLAIN&tls=true&loadBalanced=true"2
DB_NAME="<DATABASE_NAME>"
If you don’t know your connection string, see the Retrieve database credentials section above.
3. Now, in a terminal pointing to the root directory of your project, install the mongodb NPM package by running the following command:
1
npm i mongodb
4. In the same terminal, install and configure dotenv to load your environment variables by running the following command:
1
npm install dotenv
5. Then, you’ll need to import dotenv into your application so you can retrieve the environment variables stored in your .env file. To do this, at the following import statement at the very top of your application entry point (e.g., index.js):
1
import 'dotenv/config'
Let’s test to make sure that the connection is working as anticipated. Here is an example of a simple code snippet you can add to your app's main function at the entry point.
1
import "dotenv/config";2
import { MongoClient } from "mongodb";3
4
const client = new MongoClient(process.env.DB_URL);5
6
async function main() {7
try {8
await client.connect();9
const db = client.db(process.env.DB_NAME);10
11
console.log("Insert user");12
const insertUserResult = await db.collection("users").insertOne({ name: "John" });13
const insertedUserId = insertUserResult.insertedId;14
console.log(insertUserResult);15
16
console.log("Select all users");17
const selectUsersResult = await db.collection("users").find().toArray();18
console.log(selectUsersResult);19
20
console.log("Select user");21
const selectUserResult = await db.collection("users").findOne({ _id: insertedUserId });22
console.log(selectUserResult);23
24
console.log("Update user");25
const updateUserResult = await db.collection("users").updateOne({ _id: insertedUserId }, { $set: { name: "John Doe" } });26
console.log(updateUserResult);27
28
console.log("Delete user");29
const deleteUserResult = await db.collection("users").deleteOne({ _id: insertedUserId });30
console.log(deleteUserResult);31
} catch (error) {32
console.error(error);33
}34
}35
36
main();
Once you’ve saved your changes, open a terminal, navigate to your project’s root directory, and run the test script:
1
node ./index.js
This will issue a query against your SingleStore instance, allowing you to see if connectivity was successful or if further troubleshooting is required.

Start building today
Your intelligent apps are about to get even better