Docs Menu
Docs Home
/ /

MongoDB Search and MongoDB Vector Search Indexes

In this guide, you can learn how to create and manage MongoDB Search and MongoDB Vector Search indexes. These indexes allow you to use the following features:

  • MongoDB Search: Perform fast, full-text searches

  • MongoDB Vector Search: Perform semantic (similarity) searches on vector embeddings

MongoDB Search and MongoDB Vector Search indexes specify which fields to index, specify how these fields are indexed, and set other optional configurations.

Note

MongoDB Search index-management methods run asynchronously. The driver methods can return a result before the desired action completes on the server.

This guide explains how to perform the following actions to manage your MongoDB Search and MongoDB Vector Search indexes:

  • Create a Search Index Model

  • Create a Search Index

  • Create Multiple Search Indexes

  • List Search Indexes

  • Update a Search Index

  • Delete a Search Index

Note

Sample Data

The examples in this guide use the embedded_movies collection in the sample_mflix database, which is one of the Atlas sample datasets. For instructions on importing the Atlas sample data, see Load Sample Data in the Atlas documentation.

The following sections describe how to create index models for MongoDB Search and MongoDB Vector Search indexes.

To create a MongoDB Search index, you must construct a CreateSearchIndexModel instance that sets your index specifications.

The CreateSearchIndexModel class has the following properties:

Property
Type
Description

Definition

BsonDocument

Specifies the index definition. If you omit this setting, the driver creates a MongoDB Search index with dynamic mappings.

Name

string

Sets the index name. If you omit this setting, the driver sets the name to default.

Type

SearchIndexType

Sets the index type. If you omit this setting, the driver creates a MongoDB Search index by default.

The following example creates a CreateSearchIndexModel instance to provide specifications for an index named search_idx. The code specifies static mappings of the title and released fields:

var def = new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", false },
{ "fields", new BsonDocument {
{ "title", new BsonDocument { {"type", "string" } } },
{ "released", new BsonDocument { { "type", "date" } } } } }
} }
};
var indexModel = new CreateSearchIndexModel(
"search_idx",
SearchIndexType.Search,
def
);

To learn more about MongoDB Search field mappings, see Define Field Mappings in the Atlas documentation.

To create a MongoDB Vector Search index, you must construct a CreateVectorSearchIndexModel instance that sets your index specifications.

The CreateVectorSearchIndexModel class inherits from the CreateSearchIndexModel class and has the following additional properties:

Property
Type
Description

Field

FieldDefinition<TDocument>

Specifies the field that contains the vectors to index.

Similarity

VectorSimilarity

Sets the vector similarity function to use to search for the top K-nearest neighbors.

Dimensions

int

Specifies the number of dimensions that the search enforces at index-time and query-time.

FilterFields

IReadOnlyList<FieldDefinition<TDocument>>

Specifies the fields that the search uses as filters in the vector query.

Quantization

VectorQuantization?

Specifies the type of automatic vector quantization for the search vectors. If you don't set this property, the search uses no automatic quantization.

HnswMaxEdges

int?

Sets the maximum number of edges that a node can have in the Hierarchical Navigable Small Worlds graph.

HnswNumEdgeCandidates

int?

Sets the maximum number of nodes to evaluate to find the closest neighbors to connect to a new node.

The following example creates a CreateVectorSearchIndexModel instance to provide specifications for an index named vs_idx. The code specifies the embedding path as PlotEmbedding, a class property that corresponds to the plot_embedding field in MongoDB. It also indexes 1536 dimensions, and uses the Euclidean vector similarity function.

var model = new CreateVectorSearchIndexModel<Movie> (
model => model.PlotEmbedding,
"vs_idx",
VectorSimilarity.Euclidean,
1536);

To learn more about defining MongoDB Vector Search indexes, see How to Index Fields for Vector Search in the Atlas documentation.

You can use a CreateAutoEmbeddingVectorSearchIndexModel index model to create a MongoDB Vector Search index that automatically generates vector embeddings for text fields.

The CreateAutoEmbeddingVectorSearchIndexModel has the following properties, in addition to the properties inherited from CreateVectorSearchIndexModelBase<TDocument>:

Property
Type
Description

AutoEmbeddingModelName

string

Specifies the name of the embedding model to use for generating vector embeddings. For a list of supported models, see Text Embeddings in the VoyageAI documentation.

Modality

VectorEmbeddingModality

Specifies the type of data to embed. Currently, the only supported modality is VectorEmbeddingModality.Text.

The following example creates a CreateAutoEmbeddingVectorSearchIndexModel instance that provides specifications for an index named auto_embedded_index. This index uses the "voyage-4" embedding model to automatically generate vector embeddings for the plot field, and also includes optional filters for the runtime and year fields:

var model = new CreateAutoEmbeddingVectorSearchIndexModel<EmbeddedMovie>(
m => m.Plot,
"auto_embedding_index",
"voyage-4",
m => m.Runtime, m => m.Year // Optional filter fields
);

You can create a MongoDB Search or MongoDB Vector Search index on a collection by calling the SearchIndexes.CreateOne() method on an IMongoCollection instance. This method accepts an index model as a parameter, specified in a CreateSearchIndexModel or CreateVectorSearchIndexModel instance.

The following example creates a MongoDB Search index on the embedded_movies collection. The code creates a CreateSearchIndexModel that sets the index name and enables dynamic mapping. Then, the code passes the CreateSearchIndexModel instance to the SearchIndexes.CreateOne() method to create the MongoDB Search index:

var indexModel = new CreateSearchIndexModel(
"example_index",
SearchIndexType.Search,
new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", true },
} }
}
);
var result = movieCollection.SearchIndexes.CreateOne(indexModel);
Console.WriteLine("Created MongoDB Search index:\n{0}", result);
Created MongoDB Search index:
"example_index"

You can create multiple MongoDB Search and MongoDB Vector Search indexes by calling the SearchIndexes.CreateMany() method on an IMongoCollection instance. This method accepts an IEnumerable of CreateSearchIndexModel or CreateVectorSearchIndexModel instances as a parameter.

This example performs the following actions:

  1. Creates a CreateSearchIndexModel instance that specifies a MongoDB Search index named as_idx

  2. Creates a CreateVectorSearchIndexModel instance that specifies a MongoDB Vector Search index named vs_idx

  3. Passes a List of the CreateSearchIndexModel and CreateVectorSearchIndexModel instances to the SearchIndexes.CreateMany() method

  4. Creates the MongoDB Search and MongoDB Vector Search indexes on the embedded_movies collection

var searchModel = new CreateSearchIndexModel(
"as_idx",
SearchIndexType.Search,
new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", true },
} }
}
);
var vectorModel = new CreateVectorSearchIndexModel<Movie>(
m => m.PlotEmbedding,
"vs_idx",
VectorSimilarity.Euclidean,
1536);
var models = new List<CreateSearchIndexModel> { searchModel, vectorModel };
var indexes = movieCollection.SearchIndexes.CreateMany(models);
Console.WriteLine("Created Search indexes:\n{0} {1}", indexes.ToArray());
Created Search indexes:
as_idx vs_idx

You can access information about a collection's existing MongoDB Search and MongoDB Vector Search indexes by calling the SearchIndexes.List() method on the collection.

The following example accesses information about the MongoDB Search and MongoDB Vector Search indexes created in the Create Multiple Search Indexes section of this page. The code calls the SearchIndexes.List() method and prints a list of the MongoDB Search and MongoDB Vector Search indexes on the collection:

var indexesList = movieCollection.SearchIndexes.List().ToList();
foreach (var i in indexesList)
{
Console.WriteLine(i);
}
{ "id": "...", "name": "as_idx", "status": "READY", "queryable":
true, "latestDefinitionVersion": {...}, "latestDefinition": {
"mappings": { "dynamic": true } }, "statusDetail": [...] }
{ "id": "...", "name": "vs_idx", "type": "vectorSearch", "status":
"READY", "queryable": true, ..., "latestDefinition": { "fields": [{
"type": "vector", "path": "plot_embedding", "numDimensions": 1536,
"similarity": "euclidean" }] }, "statusDetail": [...] }

You can update a MongoDB Search or MongoDB Vector Search index by calling the SearchIndexes.Update() method on an IMongoCollection instance. This method accepts the following parameters:

  • Name of the index to update

  • Modified index definition document

The following example updates the Vector Search index named vs_index created in the Create Multiple Search Indexes section of this page. The code creates a new index definition document that instructs the index to use "dotProduct" as the vector similarity function. Then, the code calls the SearchIndexes.Update() method to update the index:

var updatedDef = new BsonDocument
{
{ "fields", new BsonArray
{
new BsonDocument
{
{ "type", "vector" },
{ "path", "plot_embedding" },
{ "numDimensions", 1536 },
{ "similarity", "dotProduct" }
}
}
}
};
movieCollection.SearchIndexes.Update("vs_index", updatedDef);

You can delete a MongoDB Search or MongoDB Vector Search index by calling the SearchIndexes.DropOne() method on an IMongoCollection instance. This method accepts the name of the index to delete as a parameter.

The following example deletes the MongoDB Search index named example_index created in the Create a Search Index section of this page. The code passes the index name to the SearchIndexes.DropOne() method to delete the index:

movieCollection.SearchIndexes.DropOne("example_index");

To learn about other indexes you can create by using the .NET/C# Driver, see the Create and Manage Indexes guide.

To learn more about MongoDB Search, see the following Atlas documentation:

To learn more about MongoDB Vector Search, see the following Atlas documentation:

To learn more about the methods and types mentioned in this guide, see the following API documentation:

Back

Indexes

On this page