# MongoDBGraphStore

> **Class** in `langchain_mongodb`

📖 [View in docs](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore)

GraphRAG DataStore

GraphRAG is a ChatModel that provides responses to semantic queries
based on a Knowledge Graph that an LLM is used to create.
As in Vector RAG, we augment the Chat Model's training data
with relevant information that we collect from documents.

In Vector RAG, one uses an "Embedding" model that converts both
the query, and the potentially relevant documents, into vectors,
which can then be compared, and the most similar supplied to the
Chat Model as context to the query.

In Graph RAG, one uses an "Entity-Extraction" model that converts
text into Entities and their relationships, a Knowledge Graph.
Comparison is done by Graph traversal, finding entities connected
to the query prompts. These are then supplied to the Chat Model as context.
The main difference is that GraphRAG's output is typically in a structured format.

GraphRAG excels in finding links and common entities,
even if these come from different articles. It can combine information from
distinct sources providing richer context than Vector RAG in certain cases.

Here are a few examples of so-called multi-hop questions where GraphRAG excels:
- What is the connection between ACME Corporation and GreenTech Ltd.?
- Who is leading the SolarGrid Initiative, and what is their role?
- Which organizations are participating in the SolarGrid Initiative?
- What is John Doe’s role in ACME’s renewable energy projects?
- Which company is headquartered in San Francisco and involved in the SolarGrid Initiative?

In Graph RAG, one uses an Entity-Extraction model that interprets
text documents that it is given and extracting the query,
and the potentially relevant documents, into graphs. These are
composed of nodes that are entities (nouns) and edges that are relationships.
The idea is that the graph can find connections between entities and
hence answer questions that require more than one connection.

In MongoDB, Knowledge Graphs are stored in a single Collection.
Each MongoDB Document represents a single entity (node),
and its relationships (edges) are defined in a nested field named
"relationships". The schema, and an example, are described in the
:data:`~langchain_mongodb.graphrag.prompts.entity_context` prompts module.

When a query is made, the model extracts the entities in it,
then traverses the graph to find connections.
The closest entities and their relationships form the context
that is included with the query to the Chat Model.

Consider this example Query: "Does John Doe work at MongoDB?"
GraphRAG can answer this question even if the following two statements come
from completely different sources.
- "Jane Smith works with John Doe."
- "Jane Smith works at MongoDB."

## Signature

```python
MongoDBGraphStore(
    self,
    *,
    connection_string: Optional[str] = None,
    database_name: Optional[str] = None,
    collection_name: Optional[str] = None,
    collection: Optional[Collection] = None,
    entity_extraction_model: BaseChatModel,
    entity_prompt: Optional[ChatPromptTemplate] = None,
    query_prompt: Optional[ChatPromptTemplate] = None,
    max_depth: int = 3,
    allowed_entity_types: Optional[List[str]] = None,
    allowed_relationship_types: Optional[List[str]] = None,
    entity_examples: Optional[str] = None,
    entity_name_examples: str = '',
    validate: bool = False,
    validation_action: str = 'warn',
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `connection_string` | `Optional[str]` | No | A valid MongoDB connection URI. (default: `None`) |
| `database_name` | `Optional[str]` | No | The name of the database to connect to. (default: `None`) |
| `collection_name` | `Optional[str]` | No | The name of the collection to connect to. (default: `None`) |
| `collection` | `Optional[Collection]` | No | A Collection that will represent a Knowledge Graph. ** One may pass a Collection in lieu of connection_string, database_name, and collection_name. (default: `None`) |
| `entity_extraction_model` | `BaseChatModel` | Yes | LLM for converting documents into Graph of Entities and Relationships. |
| `entity_prompt` | `Optional[ChatPromptTemplate]` | No | Prompt to fill graph store with entities following schema. Defaults to .prompts.ENTITY_EXTRACTION_INSTRUCTIONS (default: `None`) |
| `query_prompt` | `Optional[ChatPromptTemplate]` | No | Prompt extracts entities and relationships as search starting points. Defaults to .prompts.NAME_EXTRACTION_INSTRUCTIONS (default: `None`) |
| `max_depth` | `int` | No | Maximum recursion depth in graph traversal. (default: `3`) |
| `allowed_entity_types` | `Optional[List[str]]` | No | If provided, constrains search to these types. (default: `None`) |
| `allowed_relationship_types` | `Optional[List[str]]` | No | If provided, constrains search to these types. (default: `None`) |
| `entity_examples` | `Optional[str]` | No | A string containing any number of additional examples to provide as context for entity extraction. (default: `None`) |
| `entity_name_examples` | `str` | No | A string appended to prompts.NAME_EXTRACTION_INSTRUCTIONS containing examples. (default: `''`) |
| `validate` | `bool` | No | If True, entity schema will be validated on every insert or update. (default: `False`) |
| `validation_action` | `str` | No | One of {"warn", "error"}. - If "warn", the default, documents will be inserted but errors logged. - If "error", an exception will be raised if any document does not match the schema. (default: `'warn'`) |

## Constructors

```python
__init__(
    self,
    *,
    connection_string: Optional[str] = None,
    database_name: Optional[str] = None,
    collection_name: Optional[str] = None,
    collection: Optional[Collection] = None,
    entity_extraction_model: BaseChatModel,
    entity_prompt: Optional[ChatPromptTemplate] = None,
    query_prompt: Optional[ChatPromptTemplate] = None,
    max_depth: int = 3,
    allowed_entity_types: Optional[List[str]] = None,
    allowed_relationship_types: Optional[List[str]] = None,
    entity_examples: Optional[str] = None,
    entity_name_examples: str = '',
    validate: bool = False,
    validation_action: str = 'warn',
)
```

| Name | Type |
|------|------|
| `connection_string` | `Optional[str]` |
| `database_name` | `Optional[str]` |
| `collection_name` | `Optional[str]` |
| `collection` | `Optional[Collection]` |
| `entity_extraction_model` | `BaseChatModel` |
| `entity_prompt` | `Optional[ChatPromptTemplate]` |
| `query_prompt` | `Optional[ChatPromptTemplate]` |
| `max_depth` | `int` |
| `allowed_entity_types` | `Optional[List[str]]` |
| `allowed_relationship_types` | `Optional[List[str]]` |
| `entity_examples` | `Optional[str]` |
| `entity_name_examples` | `str` |
| `validate` | `bool` |
| `validation_action` | `str` |


## Properties

- `collection`
- `entity_extraction_model`
- `entity_prompt`
- `query_prompt`
- `entity_examples`
- `entity_name_examples`
- `max_depth`
- `allowed_entity_types`
- `allowed_relationship_types`
- `entity_schema`

## Methods

- [`from_connection_string()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/from_connection_string)
- [`close()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/close)
- [`add_documents()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/add_documents)
- [`extract_entities()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/extract_entities)
- [`extract_entity_names()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/extract_entity_names)
- [`find_entity_by_name()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/find_entity_by_name)
- [`related_entities()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/related_entities)
- [`similarity_search()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/similarity_search)
- [`chat_response()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/chat_response)
- [`to_networkx()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/to_networkx)
- [`view()`](https://reference.langchain.com/python/langchain-mongodb/graphrag/graph/MongoDBGraphStore/view)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-mongodb/blob/ad9050c28e092b335dcb846f77c0ec2245553f79/libs/langchain-mongodb/langchain_mongodb/graphrag/graph.py#L33)