Find answers from the community

Updated last year

Hello I am attempting to add some

Hello, I am attempting to add some metadata about my user to the documents I am uploading to mongo. I have access to the user's information, I just don't know how to add this information to the document. I imagine that I may be able to do this via the SimpleNodeParser. This what I have so far to store my data in mongo and create my index.
Plain Text
            if is_user():
                storage_context = StorageContext.from_defaults(
                    docstore=MongoDocumentStore.from_uri(uri=MONGO_URI),
                    index_store=MongoIndexStore.from_uri(uri=MONGO_URI),
                )

                parser = SimpleNodeParser()
                nodes = parser.get_nodes_from_documents(documents)

                storage_context.docstore.add_documents(nodes)

                index = VectorStoreIndex(
                    nodes, storage_context=storage_context)
L
h
6 comments
You can set the metadata on each document, and each node created from that document will inherit that metadata

documents[0].metadata = {"test": "val"}
Oh nice, thank you. I'll give it a try
As usual, @Logan M comes through. Thank you very much.
Next logical question is how I would go about querying for document for a given user and then creating an index based on those documents. This is what I came up with, but it's not working 😭
Plain Text
    client = MongoClient(MONGO_URI)
    db = client["db_docstore"]
    collection = db["documents"]

    target_user = "nick"

    query = {"metadata.user_info": target_user}

    documents_for_user = collection.find(query)

    parser = SimpleNodeParser()
    nodes = parser.get_nodes_from_documents(documents_for_user.values())

    return VectorStoreIndex(nodes)
I suppose another way of asking the above question is whether I should be using MongoDocumentStore and somehow query there? Or use MongoClient and try to construct the nodes for VectorStoreIndex?
hmmm for this part, I have no idea πŸ˜… I would expect that type of filter to would though πŸ€”
Add a reply
Sign up and join the conversation on Discord