Example 1 — Sample Data
Two games, four players each. Notice G1 has a clear high score (99)—perfect for checking $top.
db.gamescores.insertMany([
{ playerId: "PlayerA", gameId: "G1", score: 31 },
{ playerId: "PlayerB", gameId: "G1", score: 33 },
{ playerId: "PlayerC", gameId: "G1", score: 99 },
{ playerId: "PlayerD", gameId: "G1", score: 1 },
{ playerId: "PlayerA", gameId: "G2", score: 10 },
{ playerId: "PlayerB", gameId: "G2", score: 14 },
{ playerId: "PlayerC", gameId: "G2", score: 66 },
{ playerId: "PlayerD", gameId: "G2", score: 80 }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps 8 documents in gamescores. Run find() to see them:
db.gamescores.find() Read each document like this:
| Field in MongoDB | Meaning | Example |
|---|---|---|
playerId | Who played | "PlayerC" |
gameId | Which game | "G1" |
score | Points scored | 99 |
G1 scores: 99, 33, 31, 1
G2 scores: 80, 66, 14, 10
Next: $top picks the highest score per game. How It Works
- Insert once. The sample command adds 8 documents to
gamescores. - MongoDB stores fields as written. Each document keeps the same field names and values you inserted.
- find() shows the same shape. What you inserted is what you get back—ready for
$topin the next examples.

