liveBook · Manning

Embedding

As explained in MongoDB 8.0 in Action, Third Edition

A text embedding is a vector representation of text that captures semantic relationships and contextual nuances.

An embedding represents text as an array of numbers that captures meaning and context. It lets an application find related content even when a query does not use the same words. Similar items occupy nearby positions in a multidimensional space; individual numbers in the array have no useful meaning on their own. section 12.1 section 12.1.2

A similarity search

A search for “Renewable Energy” falls near “Wind Energy” and “Solar Power” in the illustrated vector space, while unrelated topics sit farther away. This is the relationship an embedding-based search uses to retrieve relevant results. section 12.1

Create embeddings outside MongoDB Atlas: Atlas stores and searches them but does not generate them. The OpenAI Embeddings API is one option; other options include models available through Hugging Face, TensorFlow Hub, and local deployments. The choice of model affects the balance between accuracy and efficiency. section 12.1.1

Listing 12.1 Creating text embeddings with the OpenAI Embeddings API
export OPENAI_API_KEY=your-api-key
curl https://api.openai.com/v1/embeddings \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "input": "MongoDB in Action 8.0",
    "model": "text-embedding-3-small"
  }'

Use the API request as a starting point: set OPENAI_API_KEY, pass the text as input, and select text-embedding-3-small. The response contains an embedding array to store with the corresponding content. section 12.1.1

To search stored content by meaning:

  • Generate an embedding for each item and store it alongside the original content in a MongoDB collection. section 1.3.2 section 12.1.2
  • Create an Atlas Vector Search index on the embedding field. Specify the vector dimensions; the stated Atlas limit is 4,096 dimensions, and higher dimensions can cost more computation. section 12.2.1
  • Embed the query with the same model used for the stored data. In a $vectorSearch aggregation stage, provide the index, embedding-field path, queryVector, candidate count, and result limit. Text alone is not a query vector. section 12.3.1

Searching movie plots

A vector search for “village life” uses an embedding of that phrase as its queryVector and searches the plot_embedding field. The returned movie plots concern village or rural life and include similarity scores. section 12.3.1

For frequently changing data, an Atlas Trigger can respond to inserts, updates, or replacements, call an external embedding API, and update the document’s embedding field. Triggers are an Atlas feature, not a core MongoDB server feature. In retrieval-augmented generation, the same pattern extends to document chunks: embed and store them, embed each question, then supply retrieved chunks as context to a language model. section 12.5 section 14.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains numerical representation and same-model query requirements without deriving embedding-model internals.
Examples
Uses an OpenAI API request and a “village life” search over embedded movie plots.
What it assumes
Introduces vectors and external embedding generation before using MongoDB collections, indexes, and aggregation queries.
What you can do after
Apply it via Atlas Vector Search to index stored embeddings and retrieve documents using an embedded query.
How it connects
Connects embeddings to vector databases, semantic search, Atlas Vector Search, and retrieval-augmented generation.

Also explained in

Compare how 47 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.