|
1 | 1 | # langraph-store-mongodb |
2 | 2 |
|
3 | 3 | LangGraph long-term memory using MongoDB. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install -U langgraph-store-mongodb |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +For more detailed usage examples and documentation, please refer to the [MongoDB LangGraph documentation](https://www.mongodb.com/docs/atlas/ai-integrations/langgraph/). |
| 14 | + |
| 15 | +```python |
| 16 | +from langgraph.store.mongodb import MongoDBStore, create_vector_index_config |
| 17 | +from langchain_voyageai import VoyageAIEmbeddings |
| 18 | + |
| 19 | +# Vector search index configuration with client-side embedding |
| 20 | +index_config = create_vector_index_config( |
| 21 | + embed = VoyageAIEmbeddings(), |
| 22 | + dims = <dimensions>, |
| 23 | + fields = ["<field-name>"], |
| 24 | + filters = ["<filter-field-name>", ...] # Optional |
| 25 | +) |
| 26 | + |
| 27 | +# Store memories in MongoDB collection |
| 28 | +with MongoDBStore.from_conn_string( |
| 29 | + conn_string=MONGODB_URI, |
| 30 | + db_name="<database-name>", |
| 31 | + collection_name="<collection-name>", |
| 32 | + index_config=index_config |
| 33 | + ) as store: |
| 34 | + store.put( |
| 35 | + namespace=("user", "memories"), |
| 36 | + key=f"memory_{hash(content)}", |
| 37 | + value={"content": content} |
| 38 | + ) |
| 39 | + |
| 40 | +# Retrieve memories from MongoDB collection |
| 41 | +with MongoDBStore.from_conn_string( |
| 42 | + conn_string=MONGODB_URI, |
| 43 | + db_name="<database-name>", |
| 44 | + collection_name="<collection-name>", |
| 45 | + index_config=index_config |
| 46 | +) as store: |
| 47 | + results = store.search( |
| 48 | + ("user", "memories"), |
| 49 | + query="<query-text>", |
| 50 | + limit=3 |
| 51 | + ) |
| 52 | + for result in results: |
| 53 | + print(result.value) |
| 54 | + |
| 55 | +# To delete memories, use store.delete(namespace, key) |
| 56 | +# To batch operations, use store.batch(ops) |
| 57 | +``` |
0 commit comments