Cosine similarity measures how closely two vectors point in the same direction by calculating the cosine of the angle between them. Vector databases use it to retrieve embeddings that are semantically similar to a query embedding. section 12.1.2
A typical workflow converts stored content and incoming queries into embeddings, stores the vectors, compares the query vector with stored vectors, and returns the closest matches. The metric emphasizes directional alignment rather than straight-line distance. section 12.1.2 section 12.2.3
Choose cosine similarity when:
- Comparing text, sentences, or documents in semantic search and natural-language-processing tasks. section 12.2.3
- Configuring an Atlas Vector Search index whose vector field contains the selected embeddings. section 12.2.3
- Contrasting directional similarity with Euclidean distance for spatial comparisons or dot product for recommendation matching. section 12.2.3
Atlas Vector Search configuration
Define a vector field with 1,536 dimensions and set its similarity to "cosine": { "type": "vector", "path": "embedding", "numDimensions": 1536, "similarity": "cosine" }. Create the index with the Atlas CLI, then verify through mongosh that its status is READY and queryable. section 14.5.3
Figure 12.2 Vector database operations. Data is processed by an embedding model, transforming it into vector embeddings such as [1.45, ..., 1.01, 0.89, 0.06]. These embeddings are stored in a vector database. Queries from the application are also transformed into embeddings before the database is searched. The most relevant results based on these query embeddings are then retrieved and returned as query results. (Image © MongoDB 2024 CC BY-NC-SA 3.0)
Follow the embedding-to-query-results workflow: process content and queries into vectors, store them, search by similarity, and return the most relevant results. section 12.1.2













