Example 1 — Sample Data + Text Index
Imagine six short article titles. We save them in articles, then create a text index on title.
db.articles.insertMany([
{ _id: 1, title: "cakes and ale" },
{ _id: 2, title: "more cakes" },
{ _id: 3, title: "bread" },
{ _id: 4, title: "some cakes" },
{ _id: 5, title: "two cakes to go" },
{ _id: 6, title: "pie" }
]);
db.articles.createIndex({ title: "text" }); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps six documents. Run find() to see them:
db.articles.find() Read the setup like this:
| Piece | Value | Plain English |
|---|---|---|
title | string | Field we will search |
| Text index | { title: "text" } | Required for $text + textScore |
How It Works
- Insert once. Six title documents land in
articles. - Create a text index.
createIndex({ title: "text" })enables full-text search on titles. - Ready for $meta. Without this index,
$textandtextScorecannot run.

