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]

mset(key_value_pairs: Sequence[Tuple[str, Document]]) β†’ None[source]#

Set the given key-value pairs.

Parameters:

key_value_pairs (list[tuple[str, Document]]) – A list of id-document pairs.

Returns:

None

Return type:

None

yield_keys(prefix: str | None = None) β†’ Iterator[str][source]#

Yield keys in the store.

Parameters:

prefix (str) – prefix of keys to retrieve.

Return type:

Iterator[str]