MongoDBStore#
- class langchain_community.storage.mongodb.MongoDBStore(connection_string: str, db_name: str, collection_name: str, *, client_kwargs: dict | None = None)[source]#
BaseStore implementation using MongoDB as the underlying store.
Examples
Create a MongoDBStore instance and perform operations on it:
# Instantiate the MongoDBStore with a MongoDB connection from langchain.storage import MongoDBStore mongo_conn_str = "mongodb://localhost:27017/" mongodb_store = MongoDBStore(mongo_conn_str, db_name="test-db", collection_name="test-collection") # Set values for keys doc1 = Document(...) doc2 = Document(...) mongodb_store.mset([("key1", doc1), ("key2", doc2)]) # Get values for keys values = mongodb_store.mget(["key1", "key2"]) # [doc1, doc2] # Iterate over keys for key in mongodb_store.yield_keys(): print(key) # Delete keys mongodb_store.mdelete(["key1", "key2"])
Initialize the MongoDBStore with a MongoDB connection string.
- Parameters:
connection_string (str) β MongoDB connection string
db_name (str) β name to use
collection_name (str) β collection name to use
client_kwargs (dict) β Keyword arguments to pass to the Mongo client
Methods
__init__
(connection_string,Β db_name,Β ...[,Β ...])Initialize the MongoDBStore with a MongoDB connection string.
amdelete
(keys)Async delete the given keys and their associated values.
amget
(keys)Async get the values associated with the given keys.
amset
(key_value_pairs)Async set the values for the given keys.
ayield_keys
(*[,Β prefix])Async get an iterator over keys that match the given prefix.
mdelete
(keys)Delete the given ids.
mget
(keys)Get the list of documents associated with the given keys.
mset
(key_value_pairs)Set the given key-value pairs.
yield_keys
([prefix])Yield keys in the store.
- __init__(connection_string: str, db_name: str, collection_name: str, *, client_kwargs: dict | None = None) None [source]#
Initialize the MongoDBStore with a MongoDB connection string.
- Parameters:
connection_string (str) β MongoDB connection string
db_name (str) β name to use
collection_name (str) β collection name to use
client_kwargs (dict) β Keyword arguments to pass to the Mongo client
- Return type:
None
- async amdelete(keys: Sequence[K]) None #
Async delete the given keys and their associated values.
- Parameters:
keys (Sequence[K]) β A sequence of keys to delete.
- Return type:
None
- async amget(keys: Sequence[K]) List[V | None] #
Async get the values associated with the given keys.
- Parameters:
keys (Sequence[K]) β A sequence of keys.
- Returns:
A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.
- Return type:
List[V | None]
- async amset(key_value_pairs: Sequence[Tuple[K, V]]) None #
Async set the values for the given keys.
- Parameters:
key_value_pairs (Sequence[Tuple[K, V]]) β A sequence of key-value pairs.
- Return type:
None
- async ayield_keys(*, prefix: str | None = None) AsyncIterator[K] | AsyncIterator[str] #
Async get an iterator over keys that match the given prefix.
- Parameters:
prefix (str) β The prefix to match.
- Yields:
Iterator[K | str] β An iterator over keys that match the given prefix. This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.
- Return type:
AsyncIterator[K] | AsyncIterator[str]
- mdelete(keys: Sequence[str]) None [source]#
Delete the given ids.
- Parameters:
keys (list[str]) β A list of keys representing Document IDs..
- Return type:
None
- mget(keys: Sequence[str]) List[Document | None] [source]#
Get the list of documents associated with the given keys.
- Parameters:
keys (list[str]) β A list of keys representing Document IDs..
- Returns:
- A list of Documents corresponding to the provided
keys, where each Document is either retrieved successfully or represented as None if not found.
- Return type:
list[Document]