Example 1 — Articles collection and search index
// Requires MongoDB Atlas with Atlas Search enabled
db.articles.drop()
db.articles.insertMany([
{
title: "MongoDB Aggregation Pipeline Guide",
description: "Learn $match, $group, $project, and more stages.",
category: "database",
tags: ["mongodb", "aggregation", "tutorial"],
publishedYear: 2024
},
{
title: "Introduction to Atlas Search",
description: "Full-text search with $search and relevance scoring.",
category: "database",
tags: ["mongodb", "search", "atlas"],
publishedYear: 2025
},
{
title: "Node.js REST API Tutorial",
description: "Build a CRUD API with Express and MongoDB.",
category: "backend",
tags: ["nodejs", "express", "api"],
publishedYear: 2023
},
{
title: "Indexing Strategies in MongoDB",
description: "B-tree, compound, and text indexes explained.",
category: "database",
tags: ["mongodb", "indexes", "performance"],
publishedYear: 2024
},
{
title: "React Hooks for Beginners",
description: "useState, useEffect, and custom hooks.",
category: "frontend",
tags: ["react", "hooks", "javascript"],
publishedYear: 2025
}
])
// Create Atlas Search index (wait until status is READY)
db.articles.createSearchIndex(
"default",
{
mappings: {
dynamic: false,
fields: {
title: { type: "string" },
description: { type: "string" },
category: { type: "string" },
tags: { type: "string" },
publishedYear: { type: "number" }
}
}
}
)
// Verify index is ready before running $search pipelines
db.articles.getSearchIndexes() How It Works
Atlas Search indexes are separate from regular MongoDB B-tree indexes. The mapping lists which fields are searchable and their types. Until the index status is READY, $search queries fail.

