langchain_community.storage.mongodb
.MongoDBStore¶
- class langchain_community.storage.mongodb.MongoDBStore(connection_string: str, db_name: str, collection_name: str, *, client_kwargs: Optional[dict] = 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)Delete the given keys and their associated values.
amget
(keys)Get the values associated with the given keys.
amset
(key_value_pairs)Set the values for the given keys.
ayield_keys
(*[, prefix])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: Optional[dict] = 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 ¶
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[Optional[V]] ¶
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[Optional[V]]
- async amset(key_value_pairs: Sequence[Tuple[K, V]]) None ¶
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: Optional[str] = None) Union[AsyncIterator[K], AsyncIterator[str]] ¶
Get an iterator over keys that match the given prefix.
- Parameters
prefix (str) – The prefix to match.
- Returns
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
Iterator[K | 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[Optional[Document]] [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]