Here's a more complex version with additional features that make it more challenging and realistic:
// Sample Data
[
{
"name": "Alice",
"money": 120,
"clan": "Alpha",
"stats": {
"kills": 30,
"deaths": 10,
"gamesPlayed": 15
},
"inventory": ["sword", "shield"],
"lastActive": ISODate("2023-11-10T10:00:00Z")
},
{
"name": "Bob",
"money": 200,
"clan": "Beta",
"stats": {
"kills": 45,
"deaths": 20,
"gamesPlayed": 25
},
"inventory": ["axe", "armor", "potion"],
"lastActive": ISODate("2023-11-11T15:00:00Z")
},
{
"name": "Charlie",
"money": 150,
"clan": "Alpha",
"stats": {
"kills": 25,
"deaths": 15,
"gamesPlayed": 20
},
"inventory": ["bow", "arrows"],
"lastActive": ISODate("2023-11-09T08:00:00Z")
}
]
// Aggregation Pipeline
db.collection.aggregate([
// Match only active players (active in last 7 days)
{
$match: {
lastActive: {
$gte: ISODate("2023-11-04T00:00:00Z")
}
}
},
// Add calculated fields
{
$addFields: {
kdr: {
$round: [
{
$divide: ["$stats.kills", {
$cond: [
{ $eq: ["$stats.deaths", 0] },
1,
"$stats.deaths"
]
}]
},
2
]
},
avgMoneyPerGame: {
$round: [
{ $divide: ["$money", "$stats.gamesPlayed"] },
2
]
},
inventorySize: { $size: "$inventory" }
}
},
// Group by clan for clan statistics
{
$group: {
_id: "$clan",
totalClanMoney: { $sum: "$money" },
avgClanKdr: { $avg: "$kdr" },
members: { $push: "$$ROOT" },
memberCount: { $sum: 1 }
}
},
// Unwind back to individual players but keep clan stats
{
$unwind: "$members"
},
// Reshape document
{
$project: {
_id: "$members._id",
name: "$members.name",
money: "$members.money",
kdr: "$members.kdr",
clanName: "$_id",
clanStats: {
totalMoney: "$totalClanMoney",
avgKdr: { $round: ["$avgClanKdr", 2] },
memberCount: "$memberCount"
},
playerScore: {
$add: [
"$members.money",
{ $multiply: ["$members.kdr", 100] },
{ $multiply: ["$members.inventorySize", 10] }
]
}
}
},
// Sort by player score
{ $sort: { playerScore: -1 } },
// Add position field
{
$setWindowFields: {
sortBy: { playerScore: -1 },
output: {
position: { $rank: {} },
percentile: {
$denseRank: {}
}
}
}
},
// Add tier based on position
{
$addFields: {
tier: {
$switch: {
branches: [
{ case: { $lte: ["$position", 10] }, then: "Diamond" },
{ case: { $lte: ["$position", 50] }, then: "Platinum" },
{ case: { $lte: ["$position", 100] }, then: "Gold" },
{ case: { $lte: ["$position", 500] }, then: "Silver" }
],
default: "Bronze"
}
}
}
}
])
This version includes:
- Complex Data Structure: Nested objects and arrays
- Date Filtering: Only including recently active players
- Calculated Statistics: KDR (Kill/Death Ratio) and average money per game
- Clan Statistics: Grouping and ungrouping to include clan-wide stats
- Complex Scoring: Combined score based on multiple factors
- Multiple Rankings: Position and percentile
- Tier System: Assigning tiers based on position
- Error Handling: Handling division by zero in KDR calculation
- Data Transformation: Complex reshaping of documents
- Multiple Aggregation Stages: Combining various MongoDB aggregation features
This makes it more challenging because it:
- Handles complex data relationships
- Performs multiple calculations
- Requires understanding of various aggregation operators
- Deals with date operations
- Includes error handling
- Manages grouped data
- Creates multiple derived fields
- Implements a scoring and ranking system
You can make it even more complex by adding:
- More sophisticated scoring algorithms
- Time-based rankings (daily/weekly/monthly)
- More complex tier requirements
- Historical position tracking
- Regional rankings
- Season-based statistics
- Achievement calculations