Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
/ / /

Run an Atlas Vector Search Query

You can use MongoDB Vector Search with the EF Core Provider to perform semantic search on data stored in Atlas. Vector search queries your data based on semantic meaning rather than keyword matches, allowing you to retrieve more relevant results. You can use MongoDB Vector Search to support use cases such as semantic search, hybrid search, and Retrieval-Augmented Generation (RAG).

To learn more about MongoDB Vector Search, see the MongoDB Vector Search Overview in the Atlas documentation.

The examples in this guide use the embedded_movies collection from the sample_mflix database. The documents in this collection use the following Movie class as a model:

public class Movie
{
public ObjectId Id { get; set; }
public string Title { get; set; } = null!;
public string Plot { get; set; } = null!;
public float[] PlotEmbedding { get; set; } = null!;
}

The embedded_movies collection is part of the sample datasets provided by Atlas. To set up a cluster and load this sample data, see the Quick Start guide.

To use MongoDB Vector Search with the EF Core Provider, define a vector field on your entity class. Configure a vector index on that field by using the IsVectorIndex() fluent API method.

The following example defines a Movie entity with a vector field named PlotEmbedding and configures a vector index on that field.

public class MoviesDbContext : DbContext
{
public DbSet<Movie> Movies { get; init; } = null!;
public MoviesDbContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Movie>(entity =>
{
entity.ToCollection("embedded_movies");
entity.Property(m => m.Title).HasElementName("title");
entity.Property(m => m.Plot).HasElementName("plot");
entity.Property(m => m.PlotEmbedding).HasElementName("plot_embedding")
.HasBinaryVectorDataType(BinaryVectorDataType.Float32);
entity.HasIndex(m => m.PlotEmbedding, "vector_index")
.IsVectorIndex(VectorSimilarity.Cosine, 1536);
});
}
}

Note

You cannot change or delete indexes by using the EF Core Provider after they are created. To change or delete an index, use the .NET/C# Driver. To learn more, see the MongoDB Search and MongoDB Vector Search Indexes guide in the .NET/C# Driver documentation.

The EF Core Provider supports vector embeddings of the following types:

The EF Core Provider supports the following array types as vector embeddings:

  • float[]

  • double[]

  • Memory<float> and ReadOnlyMemory<float>

  • Memory<double> and ReadOnlyMemory<double>

The EF Core Provider supports the following binary vector types, which reduce storage and improve query performance compared to array types:

  • BinaryVectorFloat32 — full-precision 32-bit floats

  • BinaryVectorInt8 — 8-bit quantized integers

  • BinaryVectorPackedBit — 1-bit packed binary values

The MongoDB.Driver namespace of the .NET/C# Driver contains the VectorQuantization and VectorSimilarity enums used to configure binary vector indexes.

You can use the VectorSearch() method to run a vector search query. The following example queries the embedded_movies collection for movies with a PlotEmbedding field value that is semantically similar to "time travel adventure", and then returns up to 10 matching documents.

// Replace queryVector with a 1536-dimension embedding vector from your model
float[] queryVector = new float[] { 0.1f, -0.2f, 0.3f, ... };
var results = context.Movies
.VectorSearch(m => m.PlotEmbedding, queryVector, limit: 10)
.ToList();

To learn more about MongoDB Vector Search, see the MongoDB Vector Search documentation.

For API documentation, see the following resources:

Back

Transactions

On this page