Spring Data MongoDB: Now with Vector Search and Queryable Encryption
June 23, 2025
MongoDB is pleased to announce new enhancements to the Spring Data MongoDB library with the release of version 4.5.0, increasing capabilities related to vector search, vector search index creation, and queryable encryption. Spring Data MongoDB makes it easier for developers to integrate MongoDB into their Java applications, taking advantage of a potent combination of powerful MongoDB features and familiar Spring conventions.
Vector search
Vector embeddings convert disparate types of data into numbers that capture meaning and relationships. Many types of data—words, sentences, images, even videos—can be represented by a vector embedding for use in AI applications. In MongoDB, you can easily store and index vector embeddings alongside your other document data—no need to manage a separate vector database or maintain an ETL pipeline.
In MongoDB, an aggregation pipeline consists of one or more stages that process documents, performing operations such as $count
and $group
. $vectorSearch
is an aggregation pipeline stage for handling vector retrieval. It was released in MongoDB 6.0, and improved upon in MongoDB 7.0 and 8.0. Using the $vectorSearch
stage to pre-filter your data and perform a semantic search against indexed fields, you can easily process vector embeddings in your aggregation pipeline.
Vector search indexes
Like other retrieval techniques, indexes are a key part of implementing vector search, allowing you to narrow the scope of your semantic search and exclude irrelevant vector embeddings. This is useful in an environment where it isn’t necessary to consider every vector embedding for comparison.
Let’s see how easy it is to create a vector search index with Spring Data MongoDB 4.5.0!
VectorIndex index = new VectorIndex("vector_index")
 .addVector("plotEmbedding", vector -> vector.dimensions(1536).similarity(COSINE)) 
 .addFilter("year"); 

mongoTemplate.searchIndexOps(Movie.class) 
 .createIndex(index);

As you can see, the VectorIndex
class offers intuitive methods such as addVector
and addFilter
that allow you to define exactly, with native Spring Data APIs, the vector you want to initialize.
To actually execute a search operation that leverages the index, just issue an aggregation:
VectorSearchOperation search = VectorSearchOperation.search("vector_index") 
 .searchType(VectorSearchOperation.SearchType.ENN)
 .path("plotEmbedding") 
 .vector( ... )
 .limit(10)
 .numCandidates(150)
 .withSearchScore("score"); 

AggregationResults<MovieWithSearchScore> results = mongoTemplate
 .aggregate(newAggregation(Movie.class, search), MovieWithSearchScore.class);

Leverage the power of MongoDB to run sophisticated vector search, directly from Spring.
Queryable Encryption
Support for vector search isn’t the only enhancement found in 4.5.0. Now, you can pass encryptedFields
right into your CollectionsOptions
class, giving Spring the context to understand which fields are encrypted.
This context allows Spring to leverage the power of MongoDB Queryable Encryption (QE) to keep sensitive data protected in transit, at rest, or in use. QE allows you to encrypt sensitive application data, store it securely in an encrypted state in the MongoDB database, and perform equality and range queries directly on the encrypted data.
Let’s look at how easy it is to create an encrypted collection with Spring Data MongoDB:
CollectionOptions collectionOptions = CollectionOptions.encryptedCollection(options -> options
 .queryable(encrypted(string("ssn")).algorithm("Indexed"), equality().contention(0))
 .queryable(encrypted(int32("age")).algorithm("Range"), range().contention(8).min(0).max(150))
 .queryable(encrypted(int64("address.sign")).algorithm("Range"), range().contention(2).min(-10L).max(10L))
);

mongoTemplate.createCollection(Patient.class, collectionOptions);

By declaring upfront the options allowed for different fields of the new collection, Spring and MongoDB work together to keep your data safe!
We’re excited for you to start incorporating these exciting new features into applications built with Spring Data MongoDB. Here are some resources to help you get started:
-
Explore the Spring Data MongoDB documentation
-
Check out the GitHub repository
-
Read the release notes for Spring Data MongoDB 4.5.0