MongoDBDocStore#

class langchain_mongodb.docstores.MongoDBDocStore(collection: Collection, text_key: str = 'page_content')[source]#

MongoDB Collection providing BaseStore interface.

This is meant to be treated as a key-value store: [str, Document]

In a MongoDB Collection, the field name _id is reserved for use as a primary key. Its value must be unique in the collection, is immutable, and may be of any type other than an array or regex. As this field is always indexed, it is the natural choice to hold keys.

The value will be held simply in a field called “value”. It can contain any valid BSON type.

Example key value pair: {“_id”: “foo”, “value”: “bar”}.

Methods

__init__(collection[, text_key])

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.

from_connection_string(connection_string, ...)

Construct a Key-Value Store from a MongoDB connection URI.

insert_many(texts, metadatas, ids)

Bulk insert single batch of texts, embeddings, and optionally ids.

mdelete(keys)

Delete the given keys and their associated values.

mget(keys)

Get the values associated with the given keys.

mset(key_value_pairs[, batch_size])

Set the values for the given keys.

yield_keys(*[, prefix])

Get an iterator over keys that match the given prefix.

Parameters:
  • collection (Collection)

  • text_key (str)

__init__(collection: Collection, text_key: str = 'page_content') None[source]#
Parameters:
  • collection (Collection)

  • text_key (str)

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]

classmethod from_connection_string(connection_string: str, namespace: str, **kwargs: Any) MongoDBDocStore[source]#

Construct a Key-Value Store from a MongoDB connection URI.

Parameters:
  • connection_string (str) – A valid MongoDB connection URI.

  • namespace (str) – A valid MongoDB namespace (in form f”{database}.{collection}”)

  • kwargs (Any)

Returns:

A new MongoDBDocStore instance.

Return type:

MongoDBDocStore

insert_many(texts: List[str] | Iterable[str], metadatas: List[dict] | Generator[dict, Any, Any], ids: List[str]) None[source]#

Bulk insert single batch of texts, embeddings, and optionally ids.

insert_many in PyMongo does not overwrite existing documents. Instead, it attempts to insert each document as a new document. If a document with the same _id already exists in the collection, an error will be raised for that specific document. However, other documents in the batch that do not have conflicting _ids will still be inserted.

Parameters:
  • texts (List[str] | Iterable[str])

  • metadatas (List[dict] | Generator[dict, Any, Any])

  • ids (List[str])

Return type:

None

mdelete(keys: Sequence[str]) None[source]#

Delete the given keys and their associated values.

Parameters:

keys (Sequence[str]) – A sequence of keys to delete.

Return type:

None

mget(keys: Sequence[str]) list[Document | None][source]#

Get the values associated with the given keys.

If a key is not found in the store, the corresponding value will be None. As returning None is not the default find behavior, we form a dictionary and loop over the keys.

Parameters:

keys (Sequence[str]) – A sequence of keys.

Return type:

list[Document | None]

Returns: List of values associated with the given keys.

mset(key_value_pairs: Sequence[tuple[str, Document]], batch_size: int = 100000) None[source]#

Set the values for the given keys.

Parameters:
  • key_value_pairs (Sequence[tuple[str, Document]]) – A sequence of key-value pairs.

  • batch_size (int)

Return type:

None

yield_keys(*, prefix: str | None = None) Iterator[str][source]#

Get an iterator over keys that match the given prefix.

Parameters:

prefix (str) – The prefix to match.

Yields:

Iterator[str | str] – An iterator over keys that match the given prefix. This method is allowed to return an iterator over either str depending on what makes more sense for the given store.

Return type:

Iterator[str]