MongoDB Integration Block
The MongoDB block enables seamless integration with MongoDB databases, supporting document-based CRUD operations, advanced analytics through aggregation pipelines, and specialized features like change streams and GridFS for robust data management.
Ensure a valid MongoDB connection string and credentials are configured before setting up the block to ensure connectivity.
Features
- Document Operations: Execute find, insert, update, and delete operations.
- Aggregation Pipelines: Perform complex data transformations and analytics.
- Index Management: Optimize queries with custom indexes.
- Collection Management: Dynamically manage collections.
- GridFS Support: Store and retrieve large files efficiently.
- Change Streams: Monitor real-time data updates.
Configuration
Database Connection
-
Connection Setup:
- Provide a MongoDB URI (connection string).
- Specify the target database name.
- Include authentication credentials (username/password or certificates).
- Configure SSL/TLS for secure connections, if needed.
-
Connection String Formats:
# Standard connection mongodb://username:password@host:port/database # MongoDB Atlas mongodb+srv://username:password@cluster.mongodb.net/database # Replica Set mongodb://host1:port1,host2:port2,host3:port3/database?replicaSet=myReplicaSet
For MongoDB Atlas, use mongodb+srv:// and ensure your IP address is whitelisted in the Atlas dashboard for secure access.
Flow Editor Setup
- Drag the MongoDB block from the Integrations category into your flow.
- Select an operation (e.g., Find, Insert, Update, Delete, Aggregate).
- Enter the MongoDB URI, database name, collection, and operation-specific parameters.
- Test the configuration in the Indite editor’s preview mode to verify connectivity and operation success.
Validate the connection string and credentials in preview mode to catch errors early.
Document Operations
Find Operation
Query documents with flexible filtering, sorting, and projection for precise data retrieval.
Configuration Options:
- Collection: Target collection name.
- Filter: MongoDB query syntax to select documents.
- Projection: Fields to include/exclude in results.
- Sort: Order documents by field values.
- Limit/Skip: Enable pagination.
- Options: Additional query settings (e.g., collation).
Example Queries:
// Simple find with filter
{
"collection": "users",
"filter": { "status": "active", "age": { "$gte": 21 } },
"projection": { "name": 1, "email": 1, "createdAt": 1 },
"sort": { "createdAt": -1 },
"limit": 50
}
// Complex query with multiple conditions
{
"collection": "orders",
"filter": {
"$and": [
{ "orderDate": { "$gte": "2025-01-01" } },
{ "status": { "$in": ["completed", "shipped"] } },
{ "totalAmount": { "$gt": 150 } }
]
},
"sort": { "orderDate": -1, "totalAmount": -1 }
}
// Text search
{
"collection": "products",
"filter": { "$text": { "$search": "smartphone case" } },
"projection": { "score": { "$meta": "textScore" }, "name": 1 },
"sort": { "score": { "$meta": "textScore" } }
}Insert Operation
Add single or multiple documents with validation and bulk operation support.
Configuration Options:
- Collection: Target collection.
- Document(s): Single document or array of documents.
- Options: Ordered/unordered bulk inserts, write concern.
Example Operations:
// Single document insert
{
"collection": "users",
"document": {
"name": "Alice Smith",
"email": "alice@example.com",
"status": "active",
"createdAt": new Date(),
"settings": {
"theme": "light",
"notifications": false
}
}
}
// Bulk insert
{
"collection": "products",
"documents": [
{
"name": "Tablet",
"category": "Electronics",
"price": 499.99,
"stock": 100
},
{
"name": "Headphones",
"category": "Electronics",
"price": 79.99,
"stock": 300
}
],
"options": { "ordered": false }
}Update Operation
Modify documents using MongoDB’s powerful update operators.
Configuration Options:
- Collection: Target collection.
- Filter: Query to select documents.
- Update: Operations using operators like
$set,$inc. - Options: Multi-update, upsert, write concern.
Example Operations:
// Simple field update
{
"collection": "users",
"filter": { "_id": ObjectId("...") },
"update": {
"$set": {
"status": "inactive",
"lastUpdated": new Date()
}
}
}
// Complex update with operators
{
"collection": "orders",
"filter": { "status": "pending" },
"update": {
"$set": { "status": "shipped" },
"$inc": { "updateCount": 1 },
"$push": {
"history": {
"status": "shipped",
"timestamp": new Date(),
"updatedBy": "bot"
}
}
},
"options": { "multi": true }
}
// Upsert operation
{
"collection": "userActivity",
"filter": { "userId": "user456" },
"update": {
"$inc": { "sessionCount": 1 },
"$set": { "lastActive": new Date() },
"$setOnInsert": { "createdAt": new Date() }
},
"options": { "upsert": true }
}Delete Operation
Remove documents with precise control and safety options.
Configuration Options:
- Collection: Target collection.
- Filter: Query to match documents for deletion.
- Options:
deleteOneordeleteMany, write concern.
Example Operations:
// Delete single document
{
"collection": "sessions",
"filter": { "_id": ObjectId("...") },
"deleteOne": true
}
// Delete multiple documents
{
"collection": "logs",
"filter": {
"timestamp": { "$lt": new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
},
"deleteMany": true
}
// Conditional deletion
{
"collection": "tempData",
"filter": {
"$and": [
{ "status": "expired" },
{ "createdAt": { "$lt": new Date(Date.now() - 24 * 60 * 60 * 1000) } }
]
}
}Advanced Operations
Aggregation Pipelines
Transform and analyze data with complex pipelines for powerful insights.
Example Pipelines:
// Sales analytics aggregation
{
"collection": "orders",
"pipeline": [
{
"$match": {
"orderDate": { "$gte": new Date("2025-01-01") }
}
},
{
"$group": {
"_id": {
"year": { "$year": "$orderDate" },
"month": { "$month": "$orderDate" }
},
"totalRevenue": { "$sum": "$totalAmount" },
"orderCount": { "$sum": 1 },
"avgOrderValue": { "$avg": "$totalAmount" }
}
},
{
"$sort": { "_id.year": 1, "_id.month": 1 }
},
{
"$project": {
"month": "$_id",
"totalRevenue": 1,
"orderCount": 1,
"avgOrderValue": { "$round": ["$avgOrderValue", 2] },
"_id": 0
}
}
]
}
// Customer segmentation
{
"collection": "users",
"pipeline": [
{
"$lookup": {
"from": "orders",
"localField": "_id",
"foreignField": "customerId",
"as": "orders"
}
},
{
"$addFields": {
"totalSpent": { "$sum": "$orders.totalAmount" },
"orderCount": { "$size": "$orders" }
}
},
{
"$addFields": {
"customerTier": {
"$switch": {
"branches": [
{ "case": { "$gte": ["$totalSpent", 1000] }, "then": "VIP" },
{ "case": { "$gte": ["$totalSpent", 500] }, "then": "Premium" },
{ "case": { "$gte": ["$totalSpent", 100] }, "then": "Regular" }
],
"default": "New"
}
}
}
},
{
"$group": {
"_id": "$customerTier",
"count": { "$sum": 1 },
"avgSpent": { "$avg": "$totalSpent" }
}
}
]
}Index Operations
Create and manage indexes to optimize query performance.
Example Index Operations:
// Create single field index
{
"collection": "users",
"operation": "createIndex",
"index": { "email": 1 },
"options": { "unique": true }
}
// Create compound index
{
"collection": "orders",
"operation": "createIndex",
"index": { "customerId": 1, "orderDate": -1 },
"options": { "background": true }
}
// Create text index
{
"collection": "products",
"operation": "createIndex",
"index": {
"name": "text",
"description": "text",
"tags": "text"
},
"options": {
"weights": {
"name": 10,
"description": 5,
"tags": 1
}
}
}
// List indexes
{
"collection": "users",
"operation": "listIndexes"
}MongoDB-Specific Features
Change Streams
Monitor real-time changes to collections for dynamic updates.
{
"collection": "orders",
"operation": "watch",
"pipeline": [
{
"$match": {
"operationType": { "$in": ["insert", "update"] },
"fullDocument.status": "completed"
}
}
]
}GridFS Operations
Efficiently store and retrieve large files.
// Upload file to GridFS
{
"operation": "gridfsUpload",
"filename": "report.pdf",
"metadata": {
"uploadedBy": "user789",
"category": "reports"
}
}
// Download file from GridFS
{
"operation": "gridfsDownload",
"filename": "report.pdf"
}Geospatial Queries
Perform location-based queries for spatial analysis.
// Find nearby locations
{
"collection": "stores",
"filter": {
"location": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [-73.935242, 40.730610]
},
"$maxDistance": 1000
}
}
}
}
// Find within polygon
{
"collection": "deliveries",
"filter": {
"location": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [[
[-74.0, 40.7], [-74.0, 40.8],
[-73.9, 40.8], [-73.9, 40.7],
[-74.0, 40.7]
]]
}
}
}
}
}Troubleshooting
Common Issues
- Connection Errors: Verify MongoDB URI, credentials, and network access. For Atlas, ensure IP whitelisting in the Atlas dashboard.
- Duplicate Key Errors: Handle unique index violations (e.g., duplicate email addresses).
- Validation Errors: Check collection schema validation rules.
- Performance Issues: Optimize indexes and queries; use
explain()to analyze slow queries. - WhatsApp Limitations: Ensure data formats align with WhatsApp constraints (e.g., text or
.mp4for media)—see WhatsApp Guide.
Test all operations in the Indite editor’s preview mode to identify and resolve issues before deployment.
Error Handling
Address common MongoDB errors:
- Duplicate Key Errors: Manage unique constraint violations with try-catch or error codes.
- Validation Errors: Handle schema validation failures by checking rules.
- Connection Errors: Implement retry logic for network or authentication issues.
- Write Concern Errors: Adjust write concern settings for replication reliability.
Response Mapping
Map MongoDB outputs to bot variables:
- Document Results: Extract specific fields from returned documents.
- Aggregation Results: Process pipeline outputs for analytics.
- Operation Results: Capture insert/update/delete confirmations.
- Error Information: Log and handle error details.
Best Practices
Performance
- Index Strategy: Create indexes tailored to frequent query patterns.
- Query Optimization: Use selective projections and efficient filters.
- Aggregation: Leverage pipelines for complex data processing.
- Connection Pooling: Optimize connection reuse for scalability.
Security
- Authentication: Implement strong credentials or certificate-based auth.
- Authorization: Use role-based access control (RBAC) for fine-grained permissions.
- Network Security: Enable SSL/TLS for encrypted data transfer.
- Input Validation: Sanitize and validate all user inputs.
Data Modeling
- Schema Design: Structure documents to match access patterns.
- Embedding vs. Referencing: Balance embedding for performance and referencing for flexibility.
- Denormalization: Optimize for read-heavy workloads when appropriate.
- Validation: Enforce schema validation for data consistency.
Monitoring
- Query Performance: Track slow queries with MongoDB’s profiler.
- Index Usage: Monitor index utilization with
explain(). - Connection Metrics: Watch connection pool health.
- Error Rates: Analyze error patterns for proactive fixes.
Node Display
- Operation Details: Displays operation type (find/insert/update/delete) and collection.
- Custom Operations: Shows operation name and target collection.
- Results: Reports document count and operation status.
- Errors: Provides actionable error messages and diagnostics.