A utility library for parsing, validating, and formatting MongoDB index specifications. Version 4.1.2 provides a structured model to work with MongoDB index definitions, including index keys, options (like unique, sparse, TTL), and collation. It validates index definitions and provides helpers to convert between different representations (e.g., from shell syntax to driver objects). This package is part of the MongoDB Compass project, updated alongside Compass releases, with a stable API focused on internal use. It differentiates from direct driver use by offering higher-level validation and normalization of index specs, making it useful for tools and UIs that manage MongoDB indexes. ESM-only since v4; TypeScript types are bundled.
npm install mongodb-index-modelNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Creates an IndexModel, validates the index spec, and converts it to driver and shell formats.
Switch to ES modules: update package.json type to "module" or use .mjs extension.
Call `model.validate().isValid` instead of accessing `model.isValid` directly.
Ensure to capture the returned model; modifications to the original indexSpec object will not affect the model after creation.
Double-check key paths and directions manually; the model does not connect to MongoDB to verify fields.
Replace `model.toJSON()` with `model.toShell()` for human-readable output, or with `model.toDriver()` for driver options.
Always provide an explicit `name` property to avoid unexpected index names when creating indexes via driver.
Convert the consuming file to ES module (use import instead of require) or downgrade to mongodb-index-model@3.x.
Use `import IndexModel from 'mongodb-index-model'` (default import).
Ensure `key` is an object like { field1: 1, field2: -1 }. Directions must be numeric 1 (ascending) or -1 (descending).Use `new IndexModel(...)` to create an instance.