Example 1 — Sample Data
Imagine three short notes. Each has an id and a text string. We save them in a collection named notes.
db.notes.insertMany([
{ _id: 1, text: "Call 555-0199 or 555-0100 today" },
{ _id: 2, text: "No phones here" },
{ _id: 3, text: "Codes: A12 and B34" }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps three documents in notes. Run find() to see them:
db.notes.find() Read those documents like this:
| Field in MongoDB | Example | Plain English |
|---|---|---|
_id | 1 | Note id for this lesson |
text | "Call 555-0199 or 555-0100 today" | String we will search with regex |
Note 1 text:
"Call 555-0199 or 555-0100 today"
^------^ ^------^
idx 5 idx 17
Pattern \d{3}-\d{4} finds both numbers. How It Works
- Insert once.
insertManyadds three note documents. - MongoDB stores strings as written. Spaces and hyphens count toward
idx. - find() shows the same shape. What you inserted is what you get back—ready for
$regexFindAllnext.

