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:
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!
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.
I’m getting a similar error when doing vector search: ‘PlanExecutor error during aggregation :: caused by :: Error connecting to localhost:28000 (127.0.0.1:28000) :: caused by :: onInvoke :: caused by :: Connection refused’
Are you trying to run vector search against a locally hosted database?
MongoDB Community Edition
MongoDB Atlas
Local Development
You can download the MongoDB Community Edition to develop locally, but you will not get full-text or vector search capabilities. Developing with change streams and transactions requires additional setup.
You can use the Atlas CLI to easily set up sandboxed local Atlas deployments for development and testing, including using Atlas Search and Vector Search locally. Local Atlas deployments support change streams and transactions out of the box.