liveBook · Manning

Compound operator

As explained in MongoDB 8.0 in Action, Third Edition

A compound operator is an Atlas Search operator that combines multiple search operators into one query expression.

The Atlas Search compound operator combines search operators in one $search query. Use it when a result must satisfy several search criteria rather than just one. section 11.5.1

Require three matches in an inspections search

In mongosh, run db.inspections.aggregate([{ $search: { index: "MongoDB-in-Action", compound: { must: [{ text: { query: "food", path: "business_name" } }, { text: { query: "PASS", path: "result" } }, { text: { query: "127", path: "sector" } }] } } }, { $set: { score: { $meta: "searchScore" } } }]). Each must clause must match: the business name contains “food,” the result contains “PASS,” and the sector contains “127.” The added score indicates how well a returned document matches the search criteria. section 11.5.1

Run the query against an Atlas Search index. To try compound searches without changing a live collection, use the Atlas Search Playground’s Customer Lookup environment; its setup is not persistent, though a shared snapshot URL remains valid for 30 days. section 11.5.1 section 11.7

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Works through three required text clauses and a returned relevance score, without detailing other compound clauses or scoring internals.
Examples
Uses an inspections query matching “food” in business_name, “PASS” in result, and “127” in sector.
What it assumes
Starts with connecting through mongosh and selecting a database before querying an Atlas Search index.
What you can do after
Apply it via mongosh to require several text matches in one Atlas Search query and display each result’s score.
How it connects
Links the $search aggregation stage, text operator, must clauses, Atlas Search index, and searchScore metadata.

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.