This tutorial covers the initial setup process for users who are new to MongoDB Atlas.
If you're already familiar with MongoDB Atlas, you can skip the section.
All examples run on a free cluster, and once you add a collection to your database, you'll be ready to start.
You’ll learn preprocessing to preserve document structure after loading data from a The Little Prince file, how to add and delete documents to a collection, and manage vector store.
Once the documents added, you can learn how to query your data using semantic search, index updates for filtering, and MQL operators.
By the end of this tutorial, you'll be able to integrate PyMongo with LangChain and use VectorStore.
Table of Contents
References
Environment Setup
[Note]
langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.
delete function allow specify the Document IDs to delete, so ids store the IDs of the added documents.
Check the first document ID. The number of IDs matches the number of documents, and each ID is a unique value.
In the image below, after adding documents the STORAGE SIZE of the collection increases and you can see the documents corresponding to each ID, such as ids[0] .
The embedding field is a vector representation of the text data. It is used to determine similarity to the query vector for vector search.
Query Filter
Create a Document object, add it to a collection.
from langchain_core.documents import Document
sample_document = Document(
page_content="I am leveraging my experience as a developer to provide development education and nurture many new developers.",
metadata={"source": "linkedin"},
)
sample_id = atlas.add_documents([sample_document])
TOTAL DOCUMENTS has increased from 167 to 168.
On the last page, you can see the page_content of sample_document .
Alternatively, you can add query filter, such as the source field, to view the search results.
Delete
You can specify the document IDs to delete as arguments to the delete_documents function, such as sample_id .
atlas.delete_documents(ids=sample_id)
True
If True returns, the deletion is successful.
You can see that TOTAL DOCUMENTS has decreasesd from 168 to 167 and that sample_document has been deleted.
Query vector store
Make a query related to the content of The Little Prince and see if the vector_store returns results from a search for similar documents.
The query is based on the most well-known story about the relationship between the Little Prince and the Fox.
query = "What does it mean to be tamed according to the fox?"
Semantic Search
similarity_search method performs a basic semantic search
The k parameter in the example below specifies the number of documents.
It returns a List[Document] ranked by relevance.
atlas.similarity_search(query=query, k=1)
[Document(metadata={'_id': '67b07b9602e46738df0bbb2e', 'doc_index': 21, 'chunk_index': 122}, page_content='The fox gazed at the little prince, for a long time. \n(picture)\n"Please-- tame me!" he said. \n"I want to, very much," the little prince replied. "But I have not much time. I have friends to discover, and a great many things to understand." \n"One only understands the things that one tames," said the fox. "Men have no more time to understand anything. They buy things all ready made at the shops. But there is no shop anywhere where one can buy friendship, and so men have no friends any more. If you want a friend, tame me..." \n"What must I do, to tame you?" asked the little prince.')]
Semantic Search with Score
similarity_search_with_score method also performs a semantic search.
The difference with the similarity_search method is that it returns a relevance score of documents between 0 and 1.
[(Document(metadata={'_id': '67b07b9602e46738df0bbb2e', 'doc_index': 21, 'chunk_index': 122}, page_content='The fox gazed at the little prince, for a long time. \n(picture)\n"Please-- tame me!" he said. \n"I want to, very much," the little prince replied. "But I have not much time. I have friends to discover, and a great many things to understand." \n"One only understands the things that one tames," said the fox. "Men have no more time to understand anything. They buy things all ready made at the shops. But there is no shop anywhere where one can buy friendship, and so men have no friends any more. If you want a friend, tame me..." \n"What must I do, to tame you?" asked the little prince.'),
0.8047155141830444),
(Document(metadata={'_id': '67b07b9602e46738df0bbb2a', 'doc_index': 21, 'chunk_index': 118}, page_content='"No," said the little prince. "I am looking for friends. What does that mean-- ‘tame‘?" \n"It is an act too often neglected," said the fox. It means to establish ties." \n"\'To establish ties\'?"\n"Just that," said the fox. "To me, you are still nothing more than a little boy who is just like a hundred thousand other little boys. And I have no need of you. And you, on your part, have no need of me. To you, I am nothing more than a fox like a hundred thousand other foxes. But if you tame me, then we shall need each other. To me, you will be unique in all the world. To you, I shall be unique in all the world..." \n"I am beginning to understand," said the little prince. "There is a flower... I think that she has tamed me..."'),
0.7951536178588867),
(Document(metadata={'_id': '67b07b9602e46738df0bbb29', 'doc_index': 21, 'chunk_index': 117}, page_content='"What does that mean-- ‘tame‘?" \n"You do not live here," said the fox. "What is it that you are looking for?" \n"I am looking for men," said the little prince. "What does that mean-- ‘tame‘?" \n"Men," said the fox. "They have guns, and they hunt. It is very disturbing. They also raise chickens. These are their only interests. Are you looking for chickens?" \n"No," said the little prince. "I am looking for friends. What does that mean-- ‘tame‘?" \n"It is an act too often neglected," said the fox. It means to establish ties." \n"\'To establish ties\'?"'),
0.7918769717216492)]
Semantic Search with Filtering
MongoDB Atlas supports pre-filtering your data using MongoDB Query Language(MQL) Operators.
You must update the index definition using update_vector_search_index .
[(Document(metadata={'_id': '67b07b9602e46738df0bbb2a', 'doc_index': 21, 'chunk_index': 118}, page_content='"No," said the little prince. "I am looking for friends. What does that mean-- ‘tame‘?" \n"It is an act too often neglected," said the fox. It means to establish ties." \n"\'To establish ties\'?"\n"Just that," said the fox. "To me, you are still nothing more than a little boy who is just like a hundred thousand other little boys. And I have no need of you. And you, on your part, have no need of me. To you, I am nothing more than a fox like a hundred thousand other foxes. But if you tame me, then we shall need each other. To me, you will be unique in all the world. To you, I shall be unique in all the world..." \n"I am beginning to understand," said the little prince. "There is a flower... I think that she has tamed me..."'),
0.7951536178588867),
(Document(metadata={'_id': '67b07b9602e46738df0bbb29', 'doc_index': 21, 'chunk_index': 117}, page_content='"What does that mean-- ‘tame‘?" \n"You do not live here," said the fox. "What is it that you are looking for?" \n"I am looking for men," said the little prince. "What does that mean-- ‘tame‘?" \n"Men," said the fox. "They have guns, and they hunt. It is very disturbing. They also raise chickens. These are their only interests. Are you looking for chickens?" \n"No," said the little prince. "I am looking for friends. What does that mean-- ‘tame‘?" \n"It is an act too often neglected," said the fox. It means to establish ties." \n"\'To establish ties\'?"'),
0.7918769717216492),
(Document(metadata={'_id': '67b07b9602e46738df0bbb2c', 'doc_index': 21, 'chunk_index': 120}, page_content='"My life is very monotonous," the fox said. "I hunt chickens; men hunt me. All the chickens are just alike, and all the men are just alike. And, in consequence, I am a little bored. But if you tame me, it will be as if the sun came to shine on my life . I shall know the sound of a step that will be different from all the others. Other steps send me hurrying back underneath the ground. Yours will call me, like music, out of my burrow. And then look: you see the grain-fields down yonder? I do not ea t bread. Wheat is of no use to me. The wheat fields have nothing to say to me. And that is sad. But you have hair that is the colour of gold. Think how wonderful that will be when you have tamed me! The grain, which is also golden, will bring me bac k the thought of you. And I shall love to'),
0.7739419937133789)]
CRUD Operations with PyMongo
Let's use PyMongo Collection instead of MongoDBAtlasVectorSearch for our Document CRUD Operations.
Setting up with an empty collection
Delete all documents in vector_store and start with an empty collection.
delete_documents : If you don't specify an ID, all documents added to the collection are deleted.
atlas.delete_documents()
True
If True returns, the deletion is successful.
You can see that TOTAL DOCUMENTS has decreasesd to 0.
Upsert
Splits a list of documents into page_content and metadata , then upsert them.
upsert_parallel : update documents that match the filter or insert new documents.
Internally, Document is converted to RawBSONDocument .
RawBSONDocument : represent BSON document using the raw bytes.
BSON, the binary representation of JSON, is primarily used internally by MongoDB.
texts, metadatas = zip(*[(doc.page_content, doc.metadata) for doc in split_documents])
document_manager.upsert_parallel(texts=texts, metadatas=list(metadatas))
Read with Evaluation Operators
To compare the equality, use <field> : <value> expression .
For example, $regex operator returns documents that match a regular expression.
fox_query_filter : find all documents inclues the string fox in the page_content field.
find_one_by_filter : retrieve the first document that matches the condition.
- the little prince befriends the fox
It was then that the fox appeared.
"Good morning," said the fox.
"Good morning," the little prince responded politely, although when he turned around he saw nothing.
"I am right here," the voice said, "under the apple tree."
(picture)
"Who are you?" asked the little prince, and added, "You are very pretty to look at."
"I am a fox," said the fox.
"Come and play with me," proposed the little prince. "I am so unhappy."
"I cannot play with you," the fox said. "I am not tamed."
"Ah! Please excuse me," said the little prince.
But, after some thought, he added:
"What does that mean-- ‘tame‘?"
"You do not live here," said the fox. "What is it that you are looking for?"
"I am looking for men," said the little prince. "What does that mean-- ‘tame‘?"
find : find all documents that match the condition. Passing an empty filter will return all documents.
cursor = document_manager.find(filter=fox_query_filter)
fox_story_documents = []
for doc in cursor:
fox_story_documents.append(doc)
len(fox_story_documents)
19
Update with query filter
For example, $set operator sets the value of a field in a document.
preface_query_filter : find all documents with the value 0 in the metadata.doc_index field.
update_operation : updates 0 in the document's metadata.doc_index to -1 .