ai/studio

Quantum Minds MongoDB Operators

Introduction

MongoDB operators in Quantum Minds enable you to interact with NoSQL databases through natural language, providing powerful capabilities for querying, analyzing, and transforming document-oriented data. These operators are designed to work with flexible data models and handle the unique characteristics of MongoDB databases.

Available MongoDB Operators

Operator Description Common Use Cases
MongoExecution Executes MongoDB queries Data retrieval, aggregation pipelines, complex queries
MongoChat Interactive chat interface for MongoDB Conversational data exploration, guided analysis
TextToNoSQL Converts natural language to MongoDB queries Query generation, complex filter creation

MongoExecution

The MongoExecution operator (formerly MongoDf) runs MongoDB queries against connected databases and returns the results as dataframes.

Inputs

Parameter Type Required Description
query string Yes MongoDB query in JSON format
mongodataset string Yes MongoDB connection to use
db_name string No Database name (if not in connection config)
trigger string No Optional control signal

Outputs

Parameter Type Description
type string Output format (markdown)
content string Query execution status and summary
dataframe string Query results as a structured dataframe

Example Usage

Query: '{ "find": "customers", "filter": { "status": "active", "region": "Northeast" }, "sort": { "total_spend": -1 }, "limit": 20 }'
Mongodataset: "customer_analytics"

Output: Top 20 active customers from the Northeast region sorted by total spend

Supported Query Operations

Operation Description Example
find Retrieves documents from a collection { "find": "users", "filter": { "age": { "$gt": 21 } } }
aggregate Performs aggregation pipeline operations { "aggregate": "sales", "pipeline": [{ "$group": { "_id": "$region", "total": { "$sum": "$amount" } } }] }
count Counts documents matching a filter { "count": "products", "filter": { "inStock": true } }
distinct Finds distinct values for a field { "distinct": "customers", "field": "country" }
findOne Returns a single document { "findOne": "orders", "filter": { "order_id": "12345" } }

Best Practices

MongoChat

The MongoChat operator provides an interactive chat interface for MongoDB databases, combining query generation, execution, and result presentation.

Inputs

Parameter Type Required Description
prompt string Yes Natural language query or instruction
mongodataset string Yes MongoDB connection to use
trigger string No Optional control signal

Outputs

Parameter Type Description
type string Output format (markdown)
content string Response to the query with insights

Example Usage

Prompt: "Show me the distribution of customer segments across different regions and highlight which segment has the highest average order value"
Mongodataset: "customer_analytics"

Output: Analysis of customer segments by region with highlighted insights about order values

Key Features

Best Practices

TextToNoSQL

The TextToNoSQL operator converts natural language instructions into MongoDB queries.

Inputs

Parameter Type Required Description
prompt string Yes Natural language description of the data you want to query
mongodataset string Yes MongoDB connection to use
db_name string No Database name (if not in connection config)
trigger string No Optional control signal

Outputs

Parameter Type Description
type string Output format (markdown)
content string Explanation of the generated query
query string Generated MongoDB query

Example Usage

Prompt: "Find all products that have received more than 10 reviews with an average rating above 4.5 stars, and sort them by price from lowest to highest"
Mongodataset: "e_commerce"

Output:
Content: Explanation of the query logic
Query: 
{
  "find": "products",
  "filter": {
    "reviews.count": { "$gt": 10 },
    "reviews.avg_rating": { "$gt": 4.5 }
  },
  "sort": { "price": 1 }
}

Supported Query Types

Query Type Description
Find Basic document retrieval with filters
Aggregation Multi-stage data processing pipelines
Update Document modification operations
Text Search Full-text search queries
Geospatial Location-based queries
Graph Graph relationship traversal

Best Practices

Variable Collection Names

A key feature of MongoDB operators in Quantum Minds is support for variable collection names, which enables dynamic dashboards and personalized views.

Using Variable Collection Names

Variable collection names allow you to create minds that adapt to different datasets based on session parameters:

{
  "find": "${session.collection}",
  "filter": { "owner": "${user.id}" }
}

This powerful feature supports:

  1. User-specific Views: Show each user only their relevant data
  2. Tenant Isolation: Support multi-tenant applications with separate collections
  3. Time-based Analysis: Switch between time-period collections dynamically
  4. Environment Switching: Move between development, staging, and production data

Implementation Steps

To use variable collection names:

  1. In the MongoDB operator, use the ${variable} syntax in collection references
  2. Configure session variables in the mind's settings
  3. Set up appropriate RBAC settings to control variable values
  4. Use the MINDSHARE_KEY to pass context variables during refresh

MongoDB Integration Features

Mind Builder MongoDB Integration

Quantum Minds includes specialized integration features for MongoDB:

  1. Variable Collection Support: Create minds with dynamic collection references
  2. Schema Inference: Automatic detection of MongoDB document structures
  3. Aggregation Pipeline Builder: Visual tools for creating complex pipelines
  4. Query Optimization: Suggestions for index usage and query efficiency

Security Features

When working with MongoDB data, security features include:

  1. Collection Name Encryption: Protection of sensitive collection names
  2. Field-level Security: Control which fields can be queried or returned
  3. Query Limitations: Constraints on expensive operations
  4. Access Controls: Integration with MongoDB's native authentication

Common MongoDB Operator Patterns

Basic Query Pipeline

TextToNoSQL → MongoExecution → TableToTextSummary

Interactive Analysis

MongoChat (standalone)

Cross-Database Analysis

MongoExecution → SQLExecution → PandasAiDf → TableToGraph

Document Processing

MongoExecution → TableRowProcessor → TextToNoSQL → MongoExecution

MongoDB and Lightning RAG Integration

The MongoDB operators enable seamless integration between MongoDB databases and Lightning RAG collections:

Creating RAG Collections from MongoDB

  1. Use MongoExecution to extract relevant documents
  2. Process the documents with CreateKnowledgeBase
  3. Query the resulting collection using RAG operators

Enhancing MongoDB with RAG Insights

  1. Query document collections using RAG operators
  2. Structure the insights with TableRowProcessor
  3. Store enriched data back to MongoDB using TextToNoSQL and MongoExecution

Performance Considerations

Query Optimization

Data Volume Management

Best Practices for MongoDB Operators

Schema Understanding

Query Patterns

Error Handling

Maintenance

Advanced MongoDB Techniques

Working with Nested Documents

MongoDB excels at handling nested document structures. To work effectively with nested data:

  1. Dot Notation: Use dot notation to access nested fields in filters and projections

    { "filter": { "address.city": "New York" } }
    
  2. Array Operators: Use operators like $elemMatch for array filtering

    { "filter": { "tags": { "$elemMatch": { "$eq": "premium" } } } }
    
  3. Nested Aggregations: Apply aggregations to nested fields

    { "aggregate": "customers", "pipeline": [
        { "$unwind": "$orders" },
        { "$group": { "_id": "$_id", "total_spent": { "$sum": "$orders.amount" } } }
      ]
    }
    

Geospatial Queries

For location-based analysis:

  1. Proximity Searches: Find documents near a location

    { "filter": { "location": { "$near": { "$geometry": { "type": "Point", "coordinates": [-73.9857, 40.7484] }, "$maxDistance": 1000 } } } }
    
  2. Area Searches: Find documents within a defined area

    { "filter": { "location": { "$geoWithin": { "$geometry": { "type": "Polygon", "coordinates": [[...]] } } } } }
    
  3. Distance Calculations: Calculate distances between locations

    { "aggregate": "stores", "pipeline": [
        { "$geoNear": { "near": { "type": "Point", "coordinates": [-73.9857, 40.7484] }, "distanceField": "distance", "spherical": true } }
      ]
    }
    

Text Search Capabilities

For natural language searching:

  1. Full-Text Search: Search across text fields

    { "filter": { "$text": { "$search": "organic gluten-free" } } }
    
  2. Relevance Scoring: Sort by search relevance

    { "filter": { "$text": { "$search": "smartphone" } }, "sort": { "score": { "$meta": "textScore" } } }
    
  3. Fuzzy Matching: Account for typos and variations

    { "aggregate": "products", "pipeline": [
        { "$search": { "text": { "query": "labtop", "path": "product_name", "fuzzy": { "maxEdits": 1 } } } }
      ]
    }
    

MongoDB Operator Examples

Finding Documents with Complex Filters

{
  "operator": "TextToNoSQL",
  "input": {
    "prompt": "Find all high-value customers who haven't made a purchase in the last 30 days and have at least 3 items in their wishlist",
    "mongodataset": "ecommerce",
    "trigger": "customer_analysis"
  }
}

↓

{
  "operator": "MongoExecution",
  "input": {
    "query": "{ \"find\": \"customers\", \"filter\": { \"tier\": \"high\", \"last_purchase\": { \"$lt\": { \"$date\": \"2023-09-01T00:00:00Z\" } }, \"wishlist\": { \"$size\": { \"$gte\": 3 } } } }",
    "mongodataset": "ecommerce",
    "trigger": "query_execution"
  }
}

Aggregation Pipeline Example

{
  "operator": "TextToNoSQL",
  "input": {
    "prompt": "Analyze sales by month and product category, showing total revenue, average order value, and item count",
    "mongodataset": "sales",
    "trigger": "sales_analysis"
  }
}

↓

{
  "operator": "MongoExecution",
  "input": {
    "query": "{ \"aggregate\": \"orders\", \"pipeline\": [ { \"$unwind\": \"$items\" }, { \"$group\": { \"_id\": { \"month\": { \"$month\": \"$date\" }, \"category\": \"$items.category\" }, \"revenue\": { \"$sum\": { \"$multiply\": [\"$items.price\", \"$items.quantity\"] } }, \"orders\": { \"$addToSet\": \"$_id\" }, \"item_count\": { \"$sum\": \"$items.quantity\" } } }, { \"$addFields\": { \"avg_order\": { \"$divide\": [\"$revenue\", { \"$size\": \"$orders\" }] } } }, { \"$sort\": { \"_id.month\": 1, \"revenue\": -1 } } ] }",
    "mongodataset": "sales",
    "trigger": "aggregation_execution"
  }
}

Interactive Query with MongoChat

{
  "operator": "MongoChat",
  "input": {
    "prompt": "I want to understand our customer churn patterns. Show me which customer segments have the highest churn rates, and analyze whether there's any correlation with their support ticket history.",
    "mongodataset": "customer360",
    "trigger": "churn_analysis"
  }
}

Real-World Example: Customer Analytics Dashboard

// Initial data exploration
{
  "operator": "MongoChat",
  "input": {
    "prompt": "Give me an overview of our customer database structure and the key fields available for analysis",
    "mongodataset": "customer360",
    "trigger": "schema_exploration"
  }
}

↓

// Segment active customers
{
  "operator": "TextToNoSQL",
  "input": {
    "prompt": "Find all active customers, group them by segment and region, and calculate average lifetime value, purchase frequency, and recency for each group",
    "mongodataset": "customer360",
    "trigger": "segmentation_query"
  }
}

↓

{
  "operator": "MongoExecution",
  "input": {
    "query": "$TextToNoSQL_001.output.query",
    "mongodataset": "customer360",
    "trigger": "segment_execution"
  }
}

↓

// Visualize customer segments
{
  "operator": "TableToGraphV3",
  "input": {
    "prompt": "Create a heatmap showing customer lifetime value by segment and region, with color intensity representing the value and annotations showing the count of customers in each cell",
    "dataframe": "$MongoExecution_001.output.dataframe",
    "trigger": "segment_visualization"
  }
}

↓

// Find at-risk customers
{
  "operator": "TextToNoSQL",
  "input": {
    "prompt": "Identify potentially at-risk premium customers who have been active for at least 6 months, have decreased their purchase frequency by at least 30% in the last quarter compared to their previous average, and have had at least one negative support interaction",
    "mongodataset": "customer360",
    "trigger": "at_risk_query"
  }
}

↓

{
  "operator": "MongoExecution",
  "input": {
    "query": "$TextToNoSQL_002.output.query",
    "mongodataset": "customer360",
    "trigger": "at_risk_execution"
  }
}

↓

// Generate recommendations for at-risk customers
{
  "operator": "TableRowProcessor",
  "input": {
    "prompt": "For each at-risk customer, analyze their purchase history, browsing behavior, and support interactions to generate personalized re-engagement recommendations",
    "dataframe": "$MongoExecution_002.output.dataframe",
    "trigger": "recommendation_generation"
  }
}

↓

// Create executive summary
{
  "operator": "TableToTextSummary",
  "input": {
    "prompt": "Create an executive summary of customer segment performance and at-risk customer analysis, including key metrics, trends, and recommended actions",
    "dataframe": {
      "segments": "$MongoExecution_001.output.dataframe",
      "at_risk": "$TableRowProcessor_001.output.content"
    },
    "trigger": "summary_generation"
  }
}

Integration with Other Operator Types

MongoDB and SQL Integration

Combine MongoDB and SQL data sources for comprehensive analysis:

// Query MongoDB data
{
  "operator": "TextToNoSQL",
  "input": {
    "prompt": "Get customer profile data including preferences and behavior patterns",
    "mongodataset": "customer_profiles",
    "trigger": "mongo_query"
  }
}

↓

{
  "operator": "MongoExecution",
  "input": {
    "query": "$TextToNoSQL_001.output.query",
    "mongodataset": "customer_profiles",
    "trigger": "mongo_execution"
  }
}

↓

// Query related SQL data
{
  "operator": "TextToSQL",
  "input": {
    "prompt": "Get transaction history for all customers from the past year",
    "dataset": "transactions_db",
    "trigger": "sql_query"
  }
}

↓

{
  "operator": "SQLExecution",
  "input": {
    "sql": "$TextToSQL_001.output.content",
    "dataset": "transactions_db",
    "trigger": "sql_execution"
  }
}

↓

// Combine and analyze data
{
  "operator": "PandasAiDf",
  "input": {
    "prompt": "Merge customer profile data with transaction history. Then analyze how customer preferences correlate with purchase patterns",
    "dataframe": {
      "profiles": "$MongoExecution_001.output.dataframe",
      "transactions": "$SQLExecution_001.output.content"
    },
    "trigger": "data_integration"
  }
}

MongoDB with Document Intelligence

Enhance MongoDB data with document insights:

// Extract relevant documents
{
  "operator": "RAGSummarize",
  "input": {
    "prompt": "Find information about product specifications and features in our technical documentation",
    "collection": "product_docs",
    "trigger": "document_query"
  }
}

↓

// Get product data from MongoDB
{
  "operator": "TextToNoSQL",
  "input": {
    "prompt": "Get current product inventory data including stock levels, pricing, and sales velocity",
    "mongodataset": "inventory",
    "trigger": "inventory_query"
  }
}

↓

{
  "operator": "MongoExecution",
  "input": {
    "query": "$TextToNoSQL_001.output.query",
    "mongodataset": "inventory",
    "trigger": "inventory_execution"
  }
}

↓

// Enhance product data with document insights
{
  "operator": "TableToTextSummary",
  "input": {
    "prompt": "Create enhanced product descriptions that combine inventory data with technical specifications. For each product, include inventory status, pricing, and key technical features",
    "dataframe": {
      "inventory": "$MongoExecution_001.output.dataframe",
      "specifications": "$RAGSummarize_001.output.content"
    },
    "trigger": "content_generation"
  }
}

Next Steps

Explore how MongoDB Operators can be combined with LLM Operators for enhanced analysis and content generation based on your MongoDB data.


Overview | Operator Categories | SQL Operators | Table Operators | Document Operators