Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB Atlas

How to Run $unionWith with an Atlas Search $search Query

On this page

  • Create the Atlas Search Indexes
  • Run $unionWith with $search to Search the Collections

Starting in v6.0, the MongoDB $unionWith aggregation stage supports $search inside the $unionWith pipeline option. Using $unionWith, you can combine $search results from multiple collections in the same database in the result set.

This tutorial demonstrates how to run a $unionWith query with $search against the companies and inspections collections in the sample_training database. It takes you through the following steps:

  1. Set up an Atlas Search index with dynamic mappings for the companies and inspections collections in the sample_training database.

  2. Run $unionWith query with $search to perform a union of companies with mobile in their name from the companies collection with companies with same or similar business name in the inspections collection.

Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.

Note

To run a $unionWith query with $search, your cluster must run MongoDB v6.0 or higher.

To create an Atlas Search index, you must have Project Data Access Admin or higher access to the project.

In this section, you will create an Atlas Search index named default on all the fields in the companies collection in the sample_training database. You will create another Atlas Search index named default on all the fields in the inspections collection in the sample_training database. You must perform the following steps for each collection.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If the Database Deployments page is not already displayed, click Database in the sidebar.

2
  1. Click your cluster's name.

  2. Click the Atlas Search tab.

3

Click Create Search Index.

4
  • For a guided experience, select the Atlas Search Visual Editor.

  • To edit the raw index definition, select the Atlas Search JSON Editor.

5
  1. In the Index Name field, enter default.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_training database, and select the collection.

    • To create an index for the companies collection, select companies.

    • To create an index for the inspections collection, select inspections.

6

The following index definition dynamically indexes the fields of supported types in the collection. You can use the Atlas Search Visual Editor or the Atlas Search JSON Editor in the Atlas user interface to create the index.

7
8

A modal window displays to let you know your index is building. Click the Close button.

9

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


Use the Select your language drop-down menu to set the language of the example in this section.


In this section, you will connect to your Atlas cluster and run the sample query against the indexed collections in the sample_training database.

← How to Run $lookup with an Atlas Search $search Query