Atlas Search (full-text).

Create index

In Atlas UI or via API:

{
    "mappings": {
        "dynamic": false,
        "fields": {
            "title": { "type": "string", "analyzer": "lucene.standard" },
            "body": { "type": "string" },
            "tags": { "type": "stringFacet" },
            "created": { "type": "date" }
        }
    }
}
db.posts.aggregate([
    {
        $search: {
            index: "default",
            text: {
                query: "redis tutorial",
                path: ["title", "body"],
            },
        },
    },
    { $limit: 10 },
])

Score

{ $project: { _id: 1, title: 1, score: { $meta: "searchScore" } } },
{ $sort: { score: -1 } }

Autocomplete

{
    $search: {
        autocomplete: {
            query: "red",
            path: "title",
        },
    },
}

Index field needs autocomplete type.

Fuzzy

{
    text: {
        query: "redis",
        path: "title",
        fuzzy: { maxEdits: 2 },
    },
}

Phrase

{
    phrase: {
        query: "exact phrase",
        path: "body",
    },
}

Compound

{
    $search: {
        compound: {
            must: [{ text: { query: "redis", path: "title" } }],
            should: [{ text: { query: "tutorial", path: "title", score: { boost: { value: 2 } } } }],
            filter: [{ range: { path: "created", gte: ISODate("...") } }],
            mustNot: [{ text: { query: "deprecated", path: "body" } }],
        },
    },
}

Facets

{
    $searchMeta: {
        facet: {
            operator: { text: { query: "redis", path: "body" } },
            facets: {
                tags: { type: "string", path: "tags" },
                category: { type: "string", path: "category" },
            },
        },
    },
}

For “category counts” in search results.

Highlight

{
    $search: {
        text: { query: "redis", path: "body" },
        highlight: { path: "body" },
    },
},
{ $project: { highlights: { $meta: "searchHighlights" } } }

Synonyms

{ "mappings": { ... }, "synonyms": [{ "name": "tech", "source": { "collection": "synonyms" }, "analyzer": "lucene.standard" }] }

Compared to Elasticsearch

Atlas Search: built-in, no separate cluster. Less feature-rich than ES. Good for most use cases.

OSS alternative

For self-hosted Mongo: external Elasticsearch / Meilisearch / Typesense.

Common mistakes

  • Searching unindexed field → returns nothing.
  • Forgetting path.
  • Heavy compound queries → slow.
  • Wrong analyzer for language.

Read this next

If you want my Atlas search setup, it’s at rajpoot.dev .


Building something AI-, backend-, or data-heavy and want a second pair of eyes? I do consulting and freelance work — see my projects and ways to reach me at rajpoot.dev .