liveBook · Manning

Static mapping

As explained in MongoDB 8.0 in Action, Third Edition

Static mapping is a search-index configuration that explicitly defines which document fields and subfields are indexed and how they are mapped.

Static mapping is an Atlas Search index configuration that names the fields and subfields to index. Set `mappings.dynamic` to `false` and define the paths under `mappings.fields`. If `dynamic` is omitted, it also defaults to `false`. Unlike dynamic mapping, which indexes all supported fields automatically, static mapping limits indexing to the fields you choose. section 11.3.3

Choose fields your searches frequently sort or filter on. Selective indexing can improve performance and reduce disk use, making static mapping the recommended production choice. For polymorphic data, documents whose field types do not match the index definition are not indexed for those fields; check the data before relying on those paths in searches. section 11.3.3

To create a statically mapped index with the Atlas CLI:

  • Define the cluster, database, collection, and index mappings in `index-definition.json`. Set `"dynamic": false`, then give each indexed field a type. For nested paths, define an `address` field of type `document` with its own `fields` object. section 11.4
  • Run `atlas clusters search indexes create --clusterName MongoDB-in-Action --file index-definition.json --output json`. A response marked `IN_PROGRESS` means the index is still being created; list the collection’s search indexes to check its status. section 11.4
Listing 11.2 Atlas Search index config
{
  "name": "MongoDB-in-Action",
  "database": "sample_training",
  "collectionName": "inspections",
  "mappings": {
    "dynamic": false,
    "fields": {
      "business_name": {
        "type": "string",
        "analyzer": "lucene.standard"
      },
      "date": {
        "type": "date"
      },
      "result": {
        "type": "string",
        "analyzer": "lucene.standard"
      },
      "sector": {
        "type": "string",
        "analyzer": "lucene.standard"
      },
      "address": {
        "type": "document",
        "fields": {
          "city": {
            "type": "string",
            "analyzer": "lucene.standard"
          },
          "zip": {
            "type": "string",
            "analyzer": "lucene.standard"
          },
          "street": {
            "type": "string",
            "analyzer": "lucene.standard"
          },
          "number": {
            "type": "string",
            "analyzer": "lucene.standard"
          }
        }
      }
    }
  }
}

The index configuration illustrates string and date field types, `lucene.standard` on string fields, and nested address subfields. section 11.4

An existing index can also be changed to static mapping with `db.collection.updateSearchIndex()` by supplying a configuration with `dynamic: false` and explicit `fields`. These search-index shell commands require an Atlas cluster tier of at least M10. section 11.6

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines the configuration switches and walks through an index definition and creation command, including nested fields and type-mismatch behavior.
Examples
Uses an inspections index with business, date, result, sector, and address paths, plus an Atlas CLI creation command.
What it assumes
Assumes an Atlas cluster and source collection; supplies the mapping-file structure and command needed to create the index.
What you can do after
Apply it via the Atlas CLI to create a selectively indexed search index, or update an existing index with a static configuration.
How it connects
Connects static mapping to dynamic mapping, Atlas Search indexes, field types, nested documents, and analyzers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.