Use of undefined variable: SEARCH_META?

We are getting “Use of undefined variable: SEARCH_META” when executing this query:


db.getCollection("clients").aggregate([
    {
        $search: {
            compound: {
                must: [
                    {
                        equals: {
                            value: ObjectId("62aa72d4a152e94836d05dc9"),
                            path: "externalId"
                        }
                    },
                    {
                        equals: {
                            value: false,
                            path: "isDeleted"
                        }
                    },
                    {
                        wildcard: {
                            query: '*James*',
                            path: ["email", "firstname", "lastname"],
                            allowAnalyzedField: true
                        }
                    }
                ]
            },
            "count": {
                "type": "total"
            }
        }
    },
    {
        $sort: {
            "firstname": 1
        }
    },
    {
        $project: {
            "meta": "$$SEARCH_META",
            "_id": 1,
            "email": 1,
            "firstname": 1,
            "lastname": 1
        }
    },
    {
        $skip: 0
    },
    {
        $limit: 10
    }])

Any help would be greatly appreciated

Hi @Eyad and welcome to the community!

Thank you for providing the aggregation pipeline and error details.

Could you provide the following information:

  1. If the command provided had successfully run in the past
  2. The version of MongoDB in use

As per the 4.4 changelog details, specifically SERVER-58581:

Add SEARCH_META variable that populates from mongot

The above change may not be available in prior versions which would explain the error/message being generated.

Regards,
Jason

3 Likes

Thanks @Jason_Tran,

We just started using Atlas search, it is a new command. It runs successfully though if we don’t include "meta": "$$SEARCH_META" in the project stage.

Our MongoDB version is 4.2.21

Is there any workaround to count the documents for now rather than updating to a version more 4.4.x?

Hi @Eyad,

Is there any workaround to count the documents for now rather than updating to a version more 4.4.x?

Unfortunately i’m not aware of any workaround to provide the “$$SEARCH_META” data you’re after in version 4.2.21.

Regarding the count, I believe this is also contained as part of the result data from the $searchMeta stage which is available from 4.4.9.

Just to further understand the scenario, can you advise if there are any limitations preventing you from upgrading to 4.4?

Regard,
Jason

1 Like

Eventually, we are going to update MongoDB version but for now, we have a huge code base that needs to be tested thoroughly for any breaking changes if we upgrade that we are trying to avoid for this feature.
Thanks, @Jason_Tran