Docs Menu

Connect to your cluster in mongosh.

Open mongosh in a terminal window and connect to your cluster. For detailed instructions on connecting, see Connect via mongosh.


Switch to the sample_training database.

Run the following command at mongosh prompt:

use sample_training
switched to db sample_training

Run the following $unionWith with an Atlas Search $search query.

The following queries search both the companies and inspections collections for the term mobile in the name and business_name fields respectively. The query uses the following stages:

  • $search to search for companies that include mobile in the name.
  • $unionWith to do the following:

    • Use $search stage in the sub-pipeline to search for inspections of companies that include mobile in the name.
    • Perform a union of documents from the companies and documents from the inspections collections.
  • $set stage to add a new field named source that identifies the collection of the output documents.
  • $limit stage to limit the output to 3 results from each collection.
  • $project stage to:

    • Include only the specified fields in the results.
    • Add a field named score.
What is MongoDB Atlas? →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.