> For the complete documentation index, see [llms.txt](https://nomadamas.gitbook.io/ragchain-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nomadamas.gitbook.io/ragchain-docs/ragchain-structure/db/mongodb.md).

# MongoDB

## Overview

The MongoDB class is designed to interact with a MongoDB database for storing and retrieving passage contents. It provides methods for creating, loading, saving, fetching, and searching passages in a MongoDB collection.

## Usage

### 1. Make MongoDB Instance with Your MongoDB

To start using the MongoDB class, you first need to create an instance of the class. You can do this by providing the URL of your MongoDB server, the name of your database, and the name of your collection.

```python
from RAGchain.DB import MongoDB

mongo_db = MongoDB(
    mongo_url="mongodb://localhost:27017",  # replace with your MongoDB server URL
    db_name="my_database",  # replace with your database name
    collection_name="my_collection"  # replace with your collection name
)
```

### 2. Use create\_or\_load

The `create_or_load` method is used to either create a new collection if it does not exist, or load an existing collection.

```python
mongo_db.create_or_load()
```

### 3. Save Passage

You can save passages to your MongoDB collection using the `save` method. This method accepts a list of Passage objects.

```python
passages = [...]  # replace with your list of Passage objects
mongo_db.save(passages)
```

### 4. Fetch Data

The `fetch` method allows you to retrieve passages from your MongoDB collection by their IDs. This method returns a list of Passage objects.

```python
ids = [...]  # replace with your list of IDs. They can be UUID or string.
fetched_passages = mongo_db.fetch(ids)

# Print the fetched passages
for passage in fetched_passages:
    print(passage)
```

### 5. Search Data

You can search for passages in your MongoDB collection using the `search` method. This method accepts filters such as passage ID, content, filepath, and metadatas, then returns a list of [Passage ](https://nomadamas.github.io/RAGchain/build/html/RAGchain.schema.html#module-RAGchain.schema.passage)objects that match the filters.

```python
search_results = mongo_db.search(content=["search term"], # replace "search term" with your search term
                                 my_metadata_key=["search metadata"]) # replace "my_metadata_key" with your metadata key
                                 # replace "search metadata" with your metadata search term

# Print the search results
for result in search_results:
    print(result)
```

Please replace the placeholders with your actual data. Also, make sure that your MongoDB server is running and accessible at the provided URL.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://nomadamas.gitbook.io/ragchain-docs/ragchain-structure/db/mongodb.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
