1
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
.
2
Switch to the sample_training
database.
Run the following command at mongosh
prompt:
use sample_training
switched to db sample_training
3
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.
- Use
$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
.