This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { MongoAIOptimizer } from 'mongo-ai-optimizer'; | |
| const optimizer = new MongoAIOptimizer({ | |
| claudeApiKey: process.env.CLAUDE_API_KEY, | |
| mongoUri: process.env.MONGO_URI | |
| }); | |
| const results = await optimizer.analyzeAndOptimize('orders'); | |
| console.log(results.improvements); // Shows all optimizations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Webhook triggers when query performance degrades | |
| app.post('/webhook/slow-query', async (req, res) => { | |
| const { collection, query, executionTime } = req.body; | |
| if (executionTime > THRESHOLD) { | |
| const optimization = await mongoAgent.suggest(collection, query); | |
| // Create PR with suggested changes | |
| await createGitHubPR({ | |
| title: `AI: Optimize ${collection} query`, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Same query now takes 0.4 seconds | |
| db.orders.aggregate([ | |
| { $match: { | |
| status: "pending", | |
| // AI suggested adding this filter early | |
| created: { $gte: new Date(Date.now() - 7*24*60*60*1000) } | |
| }}, | |
| // AI reordered stages for efficiency | |
| { $sort: { created: -1 } }, | |
| { $limit: 1000 }, // AI added early limit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Query taking 3.2 seconds | |
| db.orders.aggregate([ | |
| { $match: { status: "pending" } }, | |
| { $lookup: { | |
| from: 'customers', | |
| localField: 'customerId', | |
| foreignField: '_id', | |
| as: 'customer' | |
| }}, | |
| { $unwind: "$items" }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async generateOptimizations(baseline) { | |
| const prompt = ` | |
| Analyze these MongoDB query patterns and their execution stats: | |
| ${JSON.stringify(baseline, null, 2)} | |
| Consider: | |
| 1. Index intersection opportunities | |
| 2. Compound index ordering based on cardinality | |
| 3. Partial indexes for subset queries | |
| 4. Aggregation pipeline optimizations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { LangChain } from 'langchain'; | |
| import { MongoClient } from 'mongodb'; | |
| import { Claude } from '@anthropic-ai/sdk'; | |
| class MongoOptimizerAgent { | |
| constructor() { | |
| this.llm = new Claude({ | |
| model: 'claude-3-opus-20240229', | |
| temperature: 0.2 // Low temperature for consistency | |
| }); |