Registry / llm-agents / langchain-mongodb

langchain-mongodb

JSON →
library0.12.0pypypi✓ verified 24d ago

This package provides integrations for MongoDB products within the LangChain ecosystem, including VectorStore, DocumentLoader, and ChatMessageHistory capabilities. It is currently at version 0.11.0 and is actively maintained, receiving frequent updates to align with LangChain's evolving architecture and MongoDB's features.

pip install langchain-mongodb pymongo
INSTALL
IMPORT
SIG · LANGCHAIN-MONGODB
L
langchain-mongodb
llm-agentspythonv0.12.0
Install
15.6s avg
Import
2678ms
Disk
233MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.11.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 2.733s · 226.3MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 15.6s · import 2.623s · 229MB
233MB installed
● package 233MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MongoDBAtlasVectorSearch
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch
from langchain.vectorstores import MongoDBAtlasVectorSearch
MongoDB vector store integration moved to a dedicated package since LangChain's ecosystem split.
MongoDBLoader
from langchain_mongodb import MongoDBLoader
from langchain_community.document_loaders import MongoDBLoader
Document loaders specific to MongoDB are now part of the langchain-mongodb package.
MongoDBChatMessageHistory
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
from langchain_community.chat_message_histories import MongoDBChatMessageHistory
Chat message history integrations are within the dedicated langchain-mongodb package.

This quickstart demonstrates how to use `MongoDBAtlasVectorSearch` to store and query documents. It connects to a MongoDB cluster (defaults to local if env vars aren't set), initializes a vector store with a placeholder embedding model, adds documents, and performs a similarity search. Remember to replace `DummyEmbeddings` with a real embedding model (e.g., `OpenAIEmbeddings`) for production use and create a vector search index in MongoDB Atlas.

import os from pymongo import MongoClient from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch # NOTE: Replace DummyEmbeddings with a real embedding model (e.g., OpenAIEmbeddings) # For a runnable example without extra API keys, we use a placeholder. class DummyEmbeddings: def embed_documents(self, texts): # Return a list of fixed-size vectors for each text return [[0.1] * 1536 for _ in texts] def embed_query(self, text): # Return a fixed-size vector for a single query return [0.1] * 1536 # Environment variables for MongoDB connection MONGODB_ATLAS_CLUSTER_URI = os.environ.get( "MONGODB_ATLAS_CLUSTER_URI", "mongodb://localhost:27017/" ) MONGODB_DATABASE = os.environ.get("MONGODB_DATABASE", "langchain_db") MONGODB_COLLECTION = os.environ.get("MONGODB_COLLECTION", "vector_collection") # Initialize MongoDB client and collection client = MongoClient(MONGODB_ATLAS_CLUSTER_URI) collection = client[MONGODB_DATABASE][MONGODB_COLLECTION] # Initialize embedding model (replace DummyEmbeddings with e.g., OpenAIEmbeddings) # embeddings = OpenAIEmbeddings(openai_api_key=os.environ.get("OPENAI_API_KEY")) embeddings = DummyEmbeddings() # Initialize MongoDB Atlas Vector Search # Ensure 'default' index exists in MongoDB Atlas on the specified collection vector_search = MongoDBAtlasVectorSearch( collection=collection, embedding=embeddings, index_name="default", # The name of your Atlas Search Vector Index ) # Add documents to the vector store docs = [ "The quick brown fox jumps over the lazy dog.", "A group of cats is called a clowder.", "Python is a high-level, interpreted programming language." ] vector_search.add_texts(docs) print(f"Added {len(docs)} documents to MongoDB Atlas Vector Search.") # Perform a similarity search query = "animals running" results = vector_search.similarity_search(query, k=1) print(f"Similarity search results for '{query}':") for res in results: print(f"- {res.page_content}") # Clean up (optional) - remove added documents # collection.delete_many({"text": {"$in": docs}}) # print("Cleaned up documents.")
Debug
Known issues
breakingLangChain's ecosystem split led to many integrations, including MongoDB, moving from `langchain` or `langchain-community` into dedicated packages like `langchain-mongodb`. Older import paths are deprecated or will result in `ModuleNotFoundError`.
fix
Ensure `langchain-mongodb` is installed: `pip install langchain-mongodb`. Update all imports to use `from langchain_mongodb...` instead of `from langchain...` or `from langchain_community...`.
affects: LangChain versions >= 0.1.0 (with `langchain-core`), langchain-mongodb >= 0.1.0
gotcha`MongoDBAtlasVectorSearch` requires a pre-configured Atlas Search Index (type 'Vector Search') on your MongoDB collection. If the `index_name` specified in your code does not exist or is misconfigured, operations will fail.
fix
Create a vector search index in the MongoDB Atlas UI for your target collection. Ensure the `index_name` parameter in `MongoDBAtlasVectorSearch` matches the name of your Atlas Vector Search index. Configure the index to use the correct embedding field and dimensions.
affects: All versions using `MongoDBAtlasVectorSearch`
gotchaAll vector store operations (adding documents, performing similarity searches) require an instantiated embedding model. Forgetting to provide one or providing an incorrectly configured model will lead to errors.
fix
Pass a valid embedding model instance (e.g., `OpenAIEmbeddings(api_key="...")`, `HuggingFaceEmbeddings()`) to the `embedding` parameter of the `MongoDBAtlasVectorSearch` constructor.
affects: All versions
gotchaThe MongoDB connection URI (`MONGODB_ATLAS_CLUSTER_URI`) must be correctly formatted, especially for Atlas clusters (e.g., `mongodb+srv://user:pass@cluster-name.mongodb.net/`). Incorrect protocols or missing credentials will result in connection failures.
fix
Verify your connection string directly from the MongoDB Atlas UI. Ensure it includes the correct protocol, host, and authentication credentials. For local setups, `mongodb://localhost:27017/` is common.
affects: All versions
Upgrade
Version history
0.12.0latest on PyPI · released Aug 24, 2026
Audit
Dependencies
langchain-corerequiredCore LangChain functionalities
pymongorequiredOfficial MongoDB driver for Python
Agent activity
48 hits · last 30 days
node
42
OpenAI (training)
1
Resources
langchain-mongodb — pip install langchain-mongodb · libregistry