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 ( :: caused by :: onInvoke :: caused by :: Connection refused' on server

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:

        $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:

AWS / Frankfurt (eu-central-1)
M0 Sandbox (General)
Replica Set - 3 nodes
None Linked
1 search index

tryed also in the pipeline:

       * 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",
       * 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`]( returns only the results of your query. The metadata results of your [`$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`]( query.

The `$SEARCH_META` aggregation variable can be used anywhere after a [`$search`]( stage in any pipeline, but it can't be used after the [`$lookup`]( or [`$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`]( stage.

Here says the contrary:

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