For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Get Started with the MongoDB Mem0 Integration

You can integrate MongoDB with Mem0 to configure long-term memory for your AI agents that persists across conversations. This tutorial configures Mem0 to use MongoDB as its vector store, and then builds a memory-augmented assistant. In this tutorial, you perform the following tasks:

  1. Set up your environment.

  2. Use MongoDB as the Mem0 vector store.

  3. Store and retrieve memory documents.

  4. Build an assistant that personalizes its responses based on the memories that it retrieves.

Mem0 is an open-source memory layer for AI agents and assistants. Instead of passing an entire conversation history to the LLM on every request, Mem0 extracts discrete facts from conversations, stores each fact as a document, and retrieves only the most relevant facts at query time.

When you configure Mem0 to use MongoDB as its vector store, Mem0 persists these memory documents in a MongoDB collection. It uses MongoDB Vector Search for semantic retrieval and MongoDB Search for full-text keyword retrieval. Mem0 creates both indexes for you the first time that it connects to your collection.

To complete this tutorial, you must have the following resources:

  • One of the following MongoDB cluster types:

    • An Atlas cluster running MongoDB version 6.0.11, 7.0.2, or later. Ensure that your IP address is included in your Atlas project's access list.

    • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

    • A MongoDB Community cluster with Search and Vector Search installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • Python v3.10 or later.

1

Run the following commands in your terminal to create a new directory named mem0-mongodb-project and install the required dependencies:

mkdir mem0-mongodb-project
cd mem0-mongodb-project
pip install mem0ai pymongo openai python-dotenv

Mem0 requires PyMongo v4.13.2 or later. The preceding command installs the latest version of PyMongo.

2

In your mem0-mongodb-project directory, create a .env file and add the following code:

OPENAI_API_KEY="<openai-api-key>"
MONGODB_URI="<connection-string>"

Replace <connection-string> with the connection string for your Atlas cluster or local Atlas deployment.

Your connection string should use the following format:

mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net

To learn more, see Connect to a Cluster via Client Libraries.

Your connection string should use the following format:

mongodb://localhost:<port-number>/?directConnection=true

To learn more, see Connection Strings.

3

In your mem0-mongodb-project directory, create a file named main.py. Add the following code to this file to load your environment variables:

from mem0 import Memory
from openai import OpenAI
from dotenv import load_dotenv
import os
load_dotenv()

In future steps, you will add code to this file to configure Mem0 and build your assistant.

In this section, you configure Mem0 to use MongoDB as its vector store. Then, you store memories that Mem0 extracts from conversations and retrieve them by using semantic search.

1

To use MongoDB as the Mem0 vector store, add the following code to your main.py file:

config = {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o-mini",
"temperature": 0.1,
"max_tokens": 2000,
},
},
"embedder": {
"provider": "openai",
"config": {
"model": "text-embedding-3-small",
"embedding_dims": 1536,
},
},
"vector_store": {
"provider": "mongodb",
"config": {
"mongo_uri": os.environ["MONGODB_URI"],
"db_name": "mem0_db",
"collection_name": "agent_memory",
"embedding_model_dims": 1536,
},
},
}
m = Memory.from_config(config)

This code sets the following keys in the config dictionary:

  • llm: Specifies gpt-4o-mini as the model that extracts facts from conversations

  • embedder: Specifies text-embedding-3-small as the model that generates the embeddings

  • vector_store: Configures MongoDB as the vector store

The index should take about one minute to build. While it builds, the index is in an initial sync state. When it finishes building, you can start querying the data in your collection.

2

Add the following code to extract and store memories from a conversation about a user's relocation:

messages = [
{"role": "user", "content": "I'm moving to Berlin next month."},
{"role": "assistant", "content": "I'll remember that you're relocating to Berlin."},
]
result = m.add(messages, user_id="alice")
print(result)

When you pass conversation data to the add() method, Mem0 uses the LLM to extract facts from the conversation and writes each fact to your collection as a separate memory document.

3

Add the following code to store memories from a second conversation, which Mem0 adds to the same user's memory profile:

messages = [
{"role": "user", "content": "I prefer concise answers and I use Python daily."},
{"role": "assistant", "content": "Noted. I'll keep my responses brief and Python-focused."},
]
result = m.add(messages, user_id="alice")
print(result)
4

To search memory data that answers a query about the user's relocation, add the following code to your main.py file:

print("\nWaiting for the MongoDB Vector Search index to finish building...")
time.sleep(60)
results = m.search(
query="When is Alice moving?",
filters={"user_id": "alice"},
top_k=5,
)
for mem in results["results"]:
print(mem["memory"], "- score:", mem["score"])

Before running the query, the code waits for 60 seconds to allow the MongoDB Vector Search index to finish building.

5

To return the user's complete memory history without running a semantic search, add the following code to your main.py file:

all_memories = m.get_all(filters={"user_id": "alice"})
for mem in all_memories["results"]:
print(mem["id"], mem["memory"])

Tip

To inspect the stored memory documents, browse the mem0_db.agent_memory collection in the Atlas UI. To learn more, see View Collections.

This section shows how to combine memory storage and retrieval into an assistant that personalizes its responses. Before the assistant responds, it retrieves the memories that are most relevant to the current message and adds them to the system prompt. After it responds, it stores the new conversation turn so that the memories grow with every exchange.

1

In your main.py file, add the following code to define a function that searches for the memories most relevant to a message and adds those memories to the system prompt:

openai_client = OpenAI()
USER_ID = "alice"
def build_system_prompt(user_message):
# Retrieve the memories that are most relevant to the message
relevant = m.search(
query=user_message,
filters={"user_id": USER_ID},
top_k=5,
)
memory_text = "\n".join(f"- {mem['memory']}" for mem in relevant["results"])
# Add the retrieved memories to the system prompt
system_prompt = "You are a helpful personal assistant."
if memory_text:
system_prompt += (
" Use the following facts to personalize your response:\n"
f"{memory_text}"
)
return system_prompt
2

Add the following code to define a function that generates a personalized response and stores the new conversation turn as memories:

def chat(user_message):
# Generate a response
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": build_system_prompt(user_message)},
{"role": "user", "content": user_message},
],
)
assistant_message = response.choices[0].message.content
# Store the new conversation turn as memories
m.add(
[
{"role": "user", "content": user_message},
{"role": "assistant", "content": assistant_message},
],
user_id=USER_ID,
)
return assistant_message
print(chat("Can you recommend a programming meetup in my city?"))

The assistant recommends a Python meetup in Berlin because it retrieved Alice's location and language preferences from MongoDB before it assembled the prompt.

To run the main.py file, run the following command from your mem0-mongodb-project directory:

python main.py

If the command runs successfully, the output resembles the following:

# Output from the first add() method that stores relocation data
{'results': [{'id': '...', 'memory': 'User is moving to Berlin around September 2026.', 'event': 'ADD'}]}
# Output from the second add() method that stores user preferences
{'results': [{'id': '...', 'memory': 'Prefers concise answers', 'event': 'ADD'}, {'id': '...', 'memory': 'Uses Python daily', 'event': 'ADD'}]}
Waiting for the MongoDB Vector Search index to finish building...
# Output from the search() method that retrieves the relocation memory
User is moving to Berlin in September 2026. - score: 0.38553877995545677User is moving to Berlin around September 2026. - score: 0.7043201923370361
# Output from the get_all() method that returns the full memory history
<id> User is moving to Berlin around September 2026.
<id> Prefers concise answers
<id> Uses Python daily
# Output from the chat() method that asks the assistant about programming meetups
Since you're moving to Berlin around September 25, 2026, I recommend checking platforms
like Meetup.com or Eventbrite closer to your move for local Python programming meetups.
You can also follow local tech communities on social media for updates on events.

To learn more about the Mem0 configuration keys and the full set of Memory methods, see Integrate MongoDB with Mem0.

To learn more about the indexes that Mem0 creates on your collection, see Create a MongoDB Vector Search Index and Manage MongoDB Search Indexes.