Integrate MongoDB Vector Search into Your RAG AI App

MongoDB has made leaps and bounds since it was first considered Web Scale. They've built an impressive ecosystem of products ranging from serverless functions, and federated queries to streaming pipelines and now vector querying.

This walkthrough will guide you to create your first Vector Search index, load data into it, query it, compare results, then integrate into your application.

If you're more of a video learner we made this short video walking through everything end-to-end:

Create Index

Once you select which embedding model you need and have determined the dimension count, create the index in your Atlas Search page:

Load Data

We'll import our embedding model and load the data using each encoding:

Script used: https://github.com/nux-ai/content/tree/master/mongodb

Query It

Next we use a simple Atlas Search query targeting the path:

[
  {
    '$vectorSearch': {
      'index': 'default',
      'path': 'plot_embedding_384',
      'queryVector': "{{parameters.query}}",
      'numCandidates': 150,
      'limit': 10
    }
  },
  {
    '$project':
    {
      'plot': 1, 'title' : 1
    }
  }
]

Compare Results

384 dim query results:

{
    "block_id": "08d33a0c",
    "cell_name": "index_string",
    "response": [
        {
            "plot": "A true story about four Allied POWs who endure harsh treatment from their Japanese captors during World War II while being forced to build a railroad through the Burmese jungle. Ultimately ...",
            "title": "To End All Wars"
        },
        {
            "plot": "The intertwined stories of three Marines during America's battle with the Japanese in the Pacific during World War II.",
            "title": "The Pacific"
        },
        {
            "plot": "A \"Hitlerjugend\" kind of story, set in the Soviet Union during the Second World War, based on a fictitious story from the eponymous book by Vladimir Kunin. The Red Army has a gang of ...",
            "title": "Bastards"
        },
        {
            "plot": "A young Korean man arrives in Japan near the end of World War II with hopes of being a fighter pilot, but ends up on the streets battling racism, organized crime, occupying American ...",
            "title": "Fighter in the Wind"
        },
        {
            "plot": "The story of ordinary men during WWII as seen from three different points of View.",
            "title": "The Fallen"
        },
        {
            "plot": "A fantastic tale of war, love, family and ambition set in the midst of the Japanese Civil Wars of the sixteenth century.",
            "title": "Ugetsu"
        },
        {
            "plot": "A dramatization of the World War II Battle of Iwo Jima.",
            "title": "Sands of Iwo Jima"
        },
        {
            "plot": "The story of student-soldiers trying to protect a middle school during the early days of the Korean War.",
            "title": "71: Into the Fire"
        },
        {
            "plot": "Several yakuza from Tokyo are sent to Okinawa to help end a gang war. The war escalates and the Tokyo drifters decide to lay low at the beach.",
            "title": "Sonatine"
        },
        {
            "plot": "In World War II-era Korea, rival runners, one Korean (Jang Dong-gun) and one Japanese (Joe Odagiri), go to war together against the Soviets.",
            "title": "My Way"
        }
    ],
    "metadata": {
        "block_type": "data",
        "block_subtype": "mongodb",
        "runtime": 513.3232421875
    },
    "error": null,
    "status": 200,
    "run_at": "2024-02-14T19:15:27.758000"
}

768 dim query:

{
    "block_id": "ce103426",
    "cell_name": "convert_column",
    "response": [
        {
            "plot": "The intertwined stories of three Marines during America's battle with the Japanese in the Pacific during World War II.",
            "title": "The Pacific"
        },
        {
            "plot": "A young Korean man arrives in Japan near the end of World War II with hopes of being a fighter pilot, but ends up on the streets battling racism, organized crime, occupying American ...",
            "title": "Fighter in the Wind"
        },
        {
            "plot": "A true story about four Allied POWs who endure harsh treatment from their Japanese captors during World War II while being forced to build a railroad through the Burmese jungle. Ultimately ...",
            "title": "To End All Wars"
        },
        {
            "plot": "The story of ordinary men during WWII as seen from three different points of View.",
            "title": "The Fallen"
        },
        {
            "plot": "The story of student-soldiers trying to protect a middle school during the early days of the Korean War.",
            "title": "71: Into the Fire"
        },
        {
            "plot": "Story about a rapid deployment defense unit that is called into action whenever freedom is threatened.",
            "title": "Megaforce"
        },
        {
            "plot": "A dramatization of the World War II Battle of Iwo Jima.",
            "title": "Sands of Iwo Jima"
        },
        {
            "plot": "A private in the latter days of WWII on the German front struggles between his will to survive and what his superiors perceive as a battlefield instinct.",
            "title": "When Trumpets Fade"
        },
        {
            "plot": "In World War II-era Korea, rival runners, one Korean (Jang Dong-gun) and one Japanese (Joe Odagiri), go to war together against the Soviets.",
            "title": "My Way"
        },
        {
            "plot": "Four American soldiers and one Brit fighting in Europe during World War II struggle to return to Allied territory after being separated from U.S. forces during the historic Malmedy Massacre.",
            "title": "Saints and Soldiers"
        }
    ],
    "metadata": {
        "block_type": "data",
        "block_subtype": "mongodb",
        "runtime": 3260.408203125
    },
    "error": null,
    "status": 200,
    "run_at": "2024-02-14T19:15:31.099000"
}

Integrate into your Application

curl --location 'https://api.nux.ai/v1/run/workbook/13272935b31146f' \
--header 'authorization: Bearer API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "parameters": {
        "query": "story about a young man and a tiger on a ship in the middle of the ocean"
    }
}'

Resources

What will you build?

Explore templates or build your own.

Join Waitlist