The MongoDB adapter provides a production-ready NoSQL database solution for BuilderBot. Perfect for applications that need horizontal scaling, flexible schemas, and cloud deployment.
Installation
npm install @builderbot/database-mongo
The adapter uses the official MongoDB Node.js driver (mongodb v7.0+), which is included as a dependency.
Basic Usage
import { createBot , createFlow , createProvider } from '@builderbot/bot'
import { MongoAdapter as Database } from '@builderbot/database-mongo'
import { BaileysProvider as Provider } from '@builderbot/provider-baileys'
const main = async () => {
const adapterDB = new Database ({
dbUri: process . env . MONGO_DB_URI ,
dbName: process . env . MONGO_DB_NAME
})
await createBot ({
flow: createFlow ([]),
provider: createProvider ( Provider ),
database: adapterDB
})
}
main ()
Configuration
MongoDB connection string. Supports standard MongoDB URIs. Examples :
Local: mongodb://localhost:27017
MongoDB Atlas: mongodb+srv://<username>:<password>@cluster.mongodb.net
Replica Set: mongodb://host1:27017,host2:27017/db?replicaSet=rs0
Name of the database to use for storing conversation history.
Connection Examples
Local MongoDB
MongoDB Atlas
With Authentication
Environment Variables
import { MongoAdapter } from '@builderbot/database-mongo'
const adapterDB = new MongoAdapter ({
dbUri: 'mongodb://localhost:27017' ,
dbName: 'builderbot'
})
Environment Variables
Create a .env file in your project root:
MONGO_DB_URI = mongodb+srv://username:[email protected]
MONGO_DB_NAME = builderbot_production
Then load it in your application:
import 'dotenv/config'
import { MongoAdapter } from '@builderbot/database-mongo'
const adapterDB = new MongoAdapter ({
dbUri: process . env . MONGO_DB_URI ,
dbName: process . env . MONGO_DB_NAME
})
Database Schema
The adapter uses a single history collection:
interface History {
from : string // Phone number or user identifier
body : any // Message content
keyword : string [] // Trigger keywords
_id ?: ObjectId // MongoDB document ID
date ?: Date // Timestamp (auto-added)
}
Example Document
{
"_id" : ObjectId( "507f1f77bcf86cd799439011" ) ,
"from" : "1234567890" ,
"body" : "Hello" ,
"keyword" : [ "hello" , "hi" ],
"date" : ISODate( "2024-03-15T10:30:00Z" )
}
Implementation Details
The adapter automatically:
Connects to MongoDB on initialization
Creates the history collection if it doesn’t exist
Adds timestamps to all saved entries
Handles connection errors gracefully
packages/database-mongo/src/mongoAdapter.ts
class MongoAdapter extends MemoryDB {
async init () : Promise < boolean > {
const client = new MongoClient ( this . credentials . dbUri )
await client . connect ()
console . log ( '🆗 Connection successfully established' )
this . db = client . db ( this . credentials . dbName )
return true
}
async save ( ctx : History ) : Promise < void > {
const ctxWithDate = {
... ctx ,
date: new Date ()
}
await this . db . collection ( 'history' ). insertOne ( ctxWithDate )
}
async getPrevByNumber ( from : string ) : Promise < any > {
const result = await this . db
. collection ( 'history' )
. find ({ from })
. sort ({ _id: - 1 })
. limit ( 1 )
. toArray ()
return result [ 0 ]
}
}
MongoDB Atlas Setup
1. Create a Free Cluster
Go to MongoDB Atlas
Sign up or log in
Create a new project
Click “Build a Database”
Choose the FREE shared tier (M0)
Select a cloud provider and region
Click “Create Cluster”
Navigate to “Database Access” in the left sidebar
Click “Add New Database User”
Choose authentication method (Username/Password)
Set username and password
Grant “Read and write to any database” role
Click “Add User”
Navigate to “Network Access”
Click “Add IP Address”
For development: Click “Allow Access from Anywhere” (0.0.0.0/0)
For production: Add your server’s specific IP address
Click “Confirm”
4. Get Connection String
Go to “Database” and click “Connect” on your cluster
Choose “Connect your application”
Select “Node.js” driver and version “4.1 or later”
Copy the connection string
Replace <password> with your database user password
Replace <dbname> with your database name (or set it in config)
mongodb+srv://username:<password>@cluster0.xxxxx.mongodb.net/
Add indexes to improve query performance:
// In MongoDB shell or Compass
db . history . createIndex ({ from: 1 }) // Index on phone numbers
db . history . createIndex ({ date: - 1 }) // Index on date (newest first)
db . history . createIndex ({ keyword: 1 }) // Index on keywords
Or programmatically:
import { MongoAdapter } from '@builderbot/database-mongo'
const adapterDB = new MongoAdapter ({
dbUri: process . env . MONGO_DB_URI ,
dbName: process . env . MONGO_DB_NAME
})
// Wait for connection
setTimeout ( async () => {
await adapterDB . db . collection ( 'history' ). createIndex ({ from: 1 })
await adapterDB . db . collection ( 'history' ). createIndex ({ date: - 1 })
console . log ( 'Indexes created' )
}, 3000 )
TypeScript Support
import { addKeyword } from '@builderbot/bot'
import { MongoAdapter } from '@builderbot/database-mongo'
import { BaileysProvider } from '@builderbot/provider-baileys'
const welcomeFlow = addKeyword < BaileysProvider , MongoAdapter >([ 'hello' ])
. addAnswer ( 'Hi! How can I help you?' )
Connection Pooling
The MongoDB driver automatically manages connection pooling. For high-traffic applications, you can tune the pool size:
import { MongoClient } from 'mongodb'
const client = new MongoClient ( uri , {
maxPoolSize: 50 , // Maximum connections
minPoolSize: 10 , // Minimum connections
maxIdleTimeMS: 60000 // Close idle connections after 60s
})
The current adapter implementation doesn’t expose these options. For custom pool settings, you may need to modify the adapter or create a custom connection.
Error Handling
The adapter logs connection errors but doesn’t throw:
packages/database-mongo/src/mongoAdapter.ts
async init (): Promise < boolean > {
try {
const client = new MongoClient ( this . credentials . dbUri )
await client . connect ()
console . log ( '🆗 Connection successfully established' )
this . db = client . db ( this . credentials . dbName )
return true
} catch (e) {
console. log ( 'Error' , e )
return
}
}
Implement your own error handling:
const adapterDB = new MongoAdapter ({
dbUri: process . env . MONGO_DB_URI ,
dbName: process . env . MONGO_DB_NAME
})
// Wait and check connection
setTimeout (() => {
if ( ! adapterDB . db ) {
console . error ( 'Failed to connect to MongoDB' )
process . exit ( 1 )
}
}, 5000 )
Backup and Restore
Using mongodump/mongorestore
# Backup
mongodump --uri= "mongodb+srv://user:[email protected] /builderbot" --out=./backup
# Restore
mongorestore --uri= "mongodb+srv://user:[email protected] /builderbot" ./backup/builderbot
MongoDB Atlas Automated Backups
MongoDB Atlas provides automated backups for M10+ clusters (paid tiers).
Monitoring
Monitor your database using:
MongoDB Atlas Dashboard : Real-time metrics and alerts
MongoDB Compass : GUI for exploring data
Application logs : Connection status and errors
Best Practices
Use environment variables for credentials
Enable authentication in production
Restrict network access to specific IPs
Create indexes on frequently queried fields
Monitor connection pool usage
Troubleshooting
Connection Timeout
Solutions :
Check network access whitelist in MongoDB Atlas
Verify firewall settings
Ensure correct connection string
Authentication Failed
Error: Authentication failed
Solutions :
Verify username and password
Check user permissions
Ensure password doesn’t contain special characters (or URL encode them)
Database Not Found
MongoDB creates databases automatically when you first write data. No manual creation needed.
Next Steps
State Management Learn about persisting conversation state
Testing Test your bot with MongoDB