Example 1 — Sample Data
Imagine six app events: login, click, purchase, and a few more. We save them in a collection named events.
db.events.insertMany([
{ _id: 1, type: "login" },
{ _id: 2, type: "click" },
{ _id: 3, type: "purchase" },
{ _id: 4, type: "click" },
{ _id: 5, type: "logout" },
{ _id: 6, type: "click" }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps six documents in events. Run find() to see them:
db.events.find() Read those documents like this:
| Field in MongoDB | Values | Plain English |
|---|---|---|
_id | 1 … 6 | Document ids |
type | login / click / … | What kind of event it is |
6 events enter the pipeline
login · click · purchase · click · logout · click
$sampleRate: 0.33 → keep ~2 of them
(exact ids change every run) How It Works
- Insert once.
insertManyadds six event documents. - No sampling yet. All six rows are stored as written.
- find() shows the same shape. Ready for
$sampleRatenext.

