gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
Create mongodb simple aggregation code, for pase in in https://mongoplayground.net/…
- User
Create mongodb simple aggregation code, for pase in in https://mongoplayground.net/
create data, and db.collection.aggregate.
What i need is, to add new field for each object, named "position". This should be sorted by this new field. And this field should be a leaderboard position, of some value, for example money field. Player who has most money is position: 1. etc
- Assistant
Sure! Here’s a simple example you can paste into MongoPlayground:
Sample Data
[ { "name": "Alice", "money": 120 }, { "name": "Bob", "money": 200 }, { "name": "Charlie", "money": 150 }, { "name": "David", "money": 200 }, { "name": "Eve", "money": 90 } ]Aggregation Pipeline
db.collection.aggregate([ // Sort by money descending { $sort: { money: -1 } }, // Add leaderboard position { $setWindowFields: { sortBy: { money: -1 }, output: { position: { $rank: {} } } } } ])Explanation
- $sort: Sorts documents by
moneydescending. - **rank`, which gives the leaderboard position (1 for highest money, etc).
Note:
$setWindowFieldsis available in MongoDB 5.0+.
Try it in MongoPlayground (link to this example).
- $sort: Sorts documents by
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.