// Sliding Window for Time-Series Data db.userActivity.aggregate([ // Sliding window for last 30 days of user engagement { $match: { timestamp: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) } } }, { $group: { _id: { // Group by day day: { $dateToString: { format: "%Y-%m-%d", date: "$timestamp" }} }, dailyActiveUsers: { $addToSet: "$userId" }, totalEvents: { $sum: 1 } } }, // Sliding window aggregation to track trends { $setWindowFields: { sortBy: { "_id.day": 1 }, output: { movingAverageUsers: { $avg: "$dailyActiveUsers.length", window: { range: [-7, 0], unit: "day" } } } } } ])
// Optimized Social Graph Schema { _id: ObjectId("user1"), followers: [ { userId: ObjectId("user2"), followedAt: ISODate(), interaction: { // Two-pointer like tracking mutualFollows: Boolean, lastInteractionScore: Number } } ], following: [ { userId: ObjectId("user3"), followedAt: ISODate() } ] } // Efficient Friend Recommendation function findPotentialConnections(userId) { return db.users.aggregate([ { $match: { _id: userId } }, // Expand followers and following { $project: { potentialConnections: { $setIntersection: [ "$followers.userId", "$following.userId" ] } } } ]); }
// DP-Inspired Caching Strategy { _id: "user_analytics_cache", userId: ObjectId("user1"), // Memoized computation results cachedMetrics: { last30DaysEngagement: { computedAt: ISODate(), totalViews: 1000, avgSessionDuration: 5.5 }, yearlyTrends: { // Cached computation results computedAt: ISODate(), metrics: { /* pre-computed data */ } } }, // Invalidation timestamp lastUpdated: ISODate() } // DP-like Incremental Computation function updateUserAnalytics(userId) { // Check if cached result is valid const cachedResult = db.analyticsCache.findOne({ userId }); if (shouldRecompute(cachedResult)) { const newMetrics = computeComplexMetrics(userId); // Atomic update with incremental computation db.analyticsCache.updateOne( { userId }, { $set: { cachedMetrics: newMetrics, lastUpdated: new Date() } }, { upsert: true } ); } }
// Greedy Index Selection db.products.createIndex( { category: 1, price: -1, soldCount: -1 }, { // Greedy optimization partialFilterExpression: { inStock: true, price: { $gt: 100 } } } ) // Query Optimization Example function greedyQueryOptimization(filters) { // Dynamically select best index const indexes = db.products.getIndexes(); const bestIndex = indexes.reduce((best, current) => { // Greedy selection of most selective index const selectivityScore = computeIndexSelectivity(current, filters); return selectivityScore > best.selectivityScore ? { index: current, selectivityScore } : best; }, { selectivityScore: -1 }); return bestIndex.index; }
// Priority Queue-like Document Structure { _id: "global_leaderboard", topUsers: [ // Maintained like a min-heap { userId: ObjectId("user1"), score: 1000, lastUpdated: ISODate() }, // Continuously maintained top K users ], updateStrategy: { maxSize: 100, evictionPolicy: "lowest_score" } } // Efficient Leaderboard Management function updateLeaderboard(userId, newScore) { db.leaderboards.findOneAndUpdate( { _id: "global_leaderboard" }, { $push: { topUsers: { $each: [{ userId, score: newScore }], $sort: { score: -1 }, $slice: 100 // Maintain top 100 } } } ); }
// Graph-like User Connections { _id: ObjectId("user1"), connections: [ { userId: ObjectId("user2"), type: "friend", strength: 0.85, // Inspired by PageRank-like scoring connectionScore: { mutualFriends: 10, interactions: 25 } } ] } // Connection Recommendation function recommendConnections(userId) { return db.users.aggregate([ { $match: { _id: userId } }, // Graph traversal-like recommendation { $graphLookup: { from: "users", startWith: "$connections.userId", connectFromField: "connections.userId", connectToField: "_id", as: "potentialConnections", maxDepth: 2, restrictSearchWithMatch: { // Avoid already connected users _id: { $nin: existingConnections } } } } ]); }
Algorithmic Efficiency
Distributed Computing
Caching and Memoization
The above is the detailed content of Algorithmic Concepts in MongoDB Design. For more information, please follow other related articles on the PHP Chinese website!