Azure Cosmos DB for NoSQL provides support for querying items with flexible schemas and native support for JSON. It now offers vector indexing and search. This feature is designed to handle high-dimensional vectors, enabling efficient and accurate vector search at any scale. You can now store vectors directly in the documents alongside your data. Each document in your database can contain not only traditional schema-free data, but also high-dimensional vectors as other properties of the documents.Learn how to leverage the vector search capabilities of Azure Cosmos DB for NoSQL from this page. If you don’t have an Azure account, you can create a free account to get started.
Setup
You’ll first need to install the@langchain/azure-cosmosdb package:
npm
.env example
Using Azure Managed identity
If you’re using Azure Managed Identity, you can configure the credentials like this:When using Azure Managed Identity and role-based access control, you must ensure that the database and container have been created beforehand. RBAC does not provide permissions to create databases and containers. You can get more information about the permission model in the Azure Cosmos DB documentation.
Security considerations when using filters
Allowing raw user input to be concatenated into SQL-like clauses - such asWHERE ${userFilter} - introduces a critical risk of SQL injection attacks, potentially exposing unintended data or compromising your system’s integrity. To mitigate this, always use Azure Cosmos DB’s parameterized query mechanism, passing in @param placeholders, which cleanly separates the query logic from user-provided input.
Here is an example of unsafe code:
123 OR 1=1, then the query becomes SELECT * FROM c WHERE c.metadata.userId = '123' OR 1=1, which forces the condition to always be true, causing it to bypass the intended filter and delete all documents.
To prevent this injection risk, you define a placeholder like @userId and Cosmos DB binds the user input separately as a parameter, ensuring it is treated strictly as data and not executable query logic as shown below.
123 OR 1=1, the input will be treated as a literal string value to match, and not as part of the query structure.
Please refer to the official documentation on parameterized queries in Azure Cosmos DB for NoSQL for more usage examples and best practices.
Usage example
Below is an example that indexes documents from a file in Azure Cosmos DB for NoSQL, runs a vector search query, and finally uses a chain to answer a question in natural language based on the retrieved documents.Advanced search options
All search types are accessed through the unifiedsimilaritySearch and similaritySearchWithScore methods using the searchType parameter in the filter options. Search types are available as constants from AzureCosmosDBNoSQLSearchType:
Available search types:
AzureCosmosDBNoSQLSearchType.Vector(default): Standard vector similarity searchAzureCosmosDBNoSQLSearchType.VectorScoreThreshold: Vector search with minimum score filterAzureCosmosDBNoSQLSearchType.FullTextSearch: Full-text search using FullTextContains (preview)AzureCosmosDBNoSQLSearchType.FullTextRanking: Full-text search with BM25 ranking (preview)AzureCosmosDBNoSQLSearchType.Hybrid: Hybrid vector + full-text search using RRF (preview)AzureCosmosDBNoSQLSearchType.HybridScoreThreshold: Hybrid search with score threshold (preview)
defaultSearchType configuration option, so you don’t have to specify it in every query:
Vector search with score threshold
Filter results based on a minimum similarity score:Maximal Marginal Relevance (MMR) search
MMR search balances relevance with diversity in the results:Full-text and hybrid search (preview)
To use full-text or hybrid search, enable it when creating the store:Full-text search
Full-text ranking
Hybrid search
Hybrid search combines vector similarity with full-text search using Reciprocal Rank Fusion (RRF):Utility methods
Delete documents
Access the underlying container
Related
- Vector store conceptual guide
- Vector store how-to guides
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

