How to Run $unionWith
with an Atlas Search $search
Query
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:
Set up an Atlas Search index with dynamic mappings for the
companies
andinspections
collections in thesample_training
database.Run
$unionWith
query with$search
to perform a union of companies withmobile
in their name from thecompanies
collection with companies with same or similar business name in theinspections
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.
Create the Atlas Search Indexes
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.
Navigate to the Atlas Search page for your project.
If it isn't already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it isn't already displayed, select your desired project from the Projects menu in the navigation bar.
Click your cluster's name.
Click the Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
default
.Note
If you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.In the Database and Collection section, find the
sample_training
database, and select the collection.To create an index for the
companies
collection, selectcompanies
.To create an index for the
inspections
collection, selectinspections
.
Specify an index definition.
The following index definition dynamically indexes the fields of supported types in the collection. You can use the Visual Editor or the JSON Editor in the Atlas user interface to create the index.
Run $unionWith
with $search
to Search the Collections
In this section, you will connect to your Atlas cluster and run
the sample query against the indexed collections in the
sample_training
database.
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 includemobile
in the name.$unionWith
to do the following:Use
$search
stage in the sub-pipeline to search for inspections of companies that includemobile
in the name.Perform a union of documents from the
companies
and documents from theinspections
collections.
$set
stage to add a new field namedsource
that identifies the collection of the output documents.$limit
stage to limit the output to3
results from each collection.$project
stage to:Include only the specified fields in the results.
Add a field named
score
.