Search Index Lifecycle
Since v3.4.0, collection accessors and Models can create, inspect, update, drop, and wait for MongoDB Search indexes. One API covers full-text search indexes and vectorSearch indexes; use vectorSearch() to query a Vector Search index.
Requirements
- MongoDB Atlas, MongoDB Community or Enterprise 8.2+ with
mongot, or the localmongodb/mongodb-atlas-localimage. - Plain
mongoddeployments (includingmongodb-memory-server) do not support Search index commands; calls reject withSEARCH_INDEX_UNSUPPORTED. - Index builds are asynchronous on the server. Create, update, and drop return once the server accepts the command; use
waitForSearchIndex()to wait for the outcome.
API
Models expose the same seven methods.
Behavior
Updating a
vectorSearchindex depends on the deployment.On every measured deployment the server rejects a
typefield in theupdateSearchIndexcommand (IDLUnknownField), so sending one does not help. monSQLize passes a rejection through asMONGODB_ERRORwithserverCodeand never falls back on its own.Where an in-place update is not available, change the definition without downtime like this: create an index under a new name,
waitForSearchIndex(newName, { until: 'queryable' }), switch your queries to the new name, then drop the old index. Both indexes exist for a while, which uses extra resources and counts against index limits (Atlas Free tier: 3 Search indexes, Flex: 10). If a short outage is acceptable, you can instead drop the index, wait fordropped, and create it again. Searches fail until it is queryable again, and that gap grows with data size (measured on atlas-local: about 1–2 s for 50,000 documents × 64 dimensions).
status passes the server value through unchanged. Use queryable to decide whether searches can run and status to show progress.
Index type
The type comes from the server's top-level type field (typeSource: 'server'). Self-managed mongot does not return that field (MongoDB limitations). In that case the type is inferred from latestDefinition (typeSource: 'inferred'): a fields array with a type: 'vector' entry means vectorSearch, and a mappings key means search. Otherwise type is omitted and typeSource is 'unknown'; such entries never match a listSearchIndexes({ type }) filter.
waitForSearchIndex
Timeouts reject with SEARCH_INDEX_WAIT_TIMEOUT, and details[0].last holds the last observed status. Aborting signal rejects with signal.reason unchanged.
writePathPolicy
createSearchIndex, createSearchIndexes, updateSearchIndex, and dropSearchIndex are management operations, the same as createIndex(). Under model-only with management: 'inherit', Model calls are allowed and direct collection calls reject with INVALID_OPERATION. Read methods (listSearchIndexes, getSearchIndex, waitForSearchIndex) are not restricted.
Errors
Every error raised from a server response keeps the driver error as cause. Server errors without a dedicated code reject with MONGODB_ERROR; details[0] carries serverCode, serverCodeName, and serverMessage, and error.monsqlize = { operation, namespace, indexName } is attached. See Error codes.