user3358517
user3358517

Reputation: 43

Is it impossible to use "$search in $lookup result" in MongoDB Atlas?

i have a collection named 'crew'

{
    _id:
    name:
    description:
    person_id:
}

and collection named 'person'

{
    _id:
    name:
    description:
}

My goal is to make recommend system by searching crew.name, crew.description and person.name.

first, i create index for each collection ( collection > search indexes ), and i use aggregate as follows.

db.crew.aggregate([
    {
        $lookup: {
            from: 'person',
            localField: 'person_id',
            foreignField: '_id',
            as: 'person_info'
            }
    }
    , {
        $unwind: {
            path: '$person_info'
        }
    }
    , {
        $project: {
            'crewId': '$_id',
            'crewName': '$name',
            'crewDescription': '$description',
            'personName': '$person_info.name'
        }
    }
    , {
        $search: {
            "text": {
                "query": "SEARCH_WORD",
                "path": ["crewName","crewDescription","personName"]
            }
        }
    }
])

as a resutl I got a error message as follows.

MongoError: $_internalSearchBetaMongotRemote is only valid as the first stage in a pipeline.

Is it impossible to use "$search in $lookup result" in mongodb atalas?

Upvotes: 0

Views: 1116

Answers (1)

RLD
RLD

Reputation: 2005

Yes it is impossible to use $search with $lookup. As per documentation,

$search must be the first stage of any pipeline it appears in. $search cannot be used in:

  • a view definition

  • a $lookup subpipeline

  • a $facet pipeline stage

Upvotes: 1

Related Questions