Example 1 — Sample Data
Imagine six students. Each document has a class, student name, and score.
db.scores.insertMany([
{ class: "A", student: "Sam", score: 90 },
{ class: "A", student: "Ana", score: 70 },
{ class: "A", student: "Lee", score: 85 },
{ class: "B", student: "Mia", score: 60 },
{ class: "B", student: "Omar", score: 95 },
{ class: "B", student: "Zoe", score: 88 }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps 6 documents in scores. Run find() to see them:
db.scores.find() Read each document like this:
| Field in MongoDB | Meaning | Example |
|---|---|---|
class | Class / group name | "B" |
student | Student name | "Omar" |
score | Test score | 95 |
Class A scores: 90, 70, 85
Class B scores: 60, 95, 88
Next: $maxN keeps the N largest scores per class. How It Works
- Insert once. The sample command adds 6 documents to
scores. - 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
$maxNin the next examples.
Top 2 scores: class A → 90, 85; class B → 95, 88.

