Try to use aggregation + search in sub collection. PlanExecutor error during aggregation

I’m trying to execute a MongoDB aggregation pipeline with a $lookup stage that includes a $search query inside the pipeline. However, I’m encountering an error that says “HostUnreachable” with the following message:

Mongo Server error (MongoCommandException): Command failed with error 6 (HostUnreachable): 'PlanExecutor error during aggregation :: caused by :: Error connecting to localhost:28000 (127.0.0.1:28000) :: caused by :: onInvoke :: caused by :: Connection refused' on server pl-0-eu-west-1.hjah3.mongodb.net:1035.

I’m using MongoDB version 6.0 and I’m running this query against a MongoDB Atlas cluster. Here’s the aggregation pipeline I’m trying to execute:

db.distributionContent.aggregate([
    {
        $lookup: {
            "from": "experienceLocation",
            "localField": "_id",
            "foreignField": "experienceId",
            "as": "location",
            "pipeline": [{
                "$search": {
                    "text": {
                        "query": "croatia"
                    }
                }
            }]
        }
    }
])

what’s wrong? The text indexes exist.

I’ve checked my MongoDB URI and it seems to be correct. The error message suggests that the pipeline is trying to connect to localhost on port 28000, but I’m not sure why it’s doing that, as I have no problem running simple queries

Any help or insights into resolving this issue would be greatly appreciated. Thank you!

Hi Serena_C

Just to confirm – are you using Atlas? $search is only available in Atlas.

I see one potential problem is that you don’t have a “path” defined for the text query. Try adding the field name you want to search in the path.

Yes it’s atlas:

VERSION
6.0.13
REGION
AWS / Frankfurt (eu-central-1)
CLUSTER TIER
M0 Sandbox (General)
TYPE
Replica Set - 3 nodes
BACKUPS
Inactive
LINKED APP SERVICES
None Linked
ATLAS SQL
Connect
ATLAS SEARCH
1 search index

tryed also in the pipeline:

[
  {
    $lookup:
      /**
       * from: The target collection.
       * localField: The local join field.
       * foreignField: The target join field.
       * as: The name for the results.
       * pipeline: Optional pipeline to run on the foreign collection.
       * let: Optional variables to use in the pipeline field stages.
       */
      {
        from: "experienceLocation",
        localField: "id",
        foreignField: "experienceId",
        as: "location",
      },
  },
  {
    $search:
      /**
       * index: The name of the Search index.
       * text: Analyzed search, with required fields of query and path, the analyzed field(s) to search.
       * compound: Combines ops.
       * span: Find in text field regions.
       * exists: Test for presence of a field.
       * near: Find near number or date.
       * range: Find in numeric or date range.
       */
      {
        index: "ExperienceLocation_TextIndex",
        text: {
          query: "croatia",
          path: {
            wildcard: "*",
          },
        },
      },
  },
]

but the error is

Reason: $_internalSearchMongotRemote is only valid as the first stage in a pipeline

I found some discrepancy in the official documentation:

[`$search`](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#mongodb-pipeline-pipe.-search) returns only the results of your query. The metadata results of your [`$search`](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#mongodb-pipeline-pipe.-search) query are saved in the `$SEARCH_META` aggregation variable. You can use the `$SEARCH_META` variable to view the metadata results for your [`$search`](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#mongodb-pipeline-pipe.-search) query.

The `$SEARCH_META` aggregation variable can be used anywhere after a [`$search`](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#mongodb-pipeline-pipe.-search) stage in any pipeline, but it can't be used after the [`$lookup`](https://www.mongodb.com/docs/master/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup) or [`$unionWith`](https://www.mongodb.com/docs/master/reference/operator/aggregation/unionWith/#mongodb-pipeline-pipe.-unionWith) stage in any pipeline. Starting in MongoDB 6.0, the `$SEARCH_META` aggregation variable can't be used in any subsequent stage after a [`$searchMeta`](https://www.mongodb.com/docs/atlas/atlas-search/query-syntax/#mongodb-pipeline-pipe.-searchMeta) stage.

Here says the contrary:

Starting in v6.0, the MongoDB 
$lookup
 aggregation stage supports $search inside the 
$lookup
 pipeline option. Using 
$lookup
, you can join multiple collections in the same database at query-time and run a $search query to further narrow down your search.