Since Mongo only supports one $text
field per aggregation pipeline (inside the first $match
stage), that means you can't perform a logical AND, since you can't $and
the results of multiple $text
searches.
// Fails due to "too many text expressions"
db.Employees.aggregate([
{$match: {$and: [
{$text: {$search: "senior"}},
{$text: {$search: "manager"}}
]}}
])
Therefore I need to perform multiple separate $text
searches, combine the results in my NodeJS code, and pass that result set back into an aggregation pipeline for further processing (e.g. $addFields
, $match
, $sort
).
Is there a way to do something like...
let results1 = db.Employees.find({"$text":{"$search":"senior"}}, {"score":{"$meta":"textScore"}})
let results2 = db.Employees.find({"$text":{"$search":"manager"}}, {"score":{"$meta":"textScore"}})
let combinedResults = _.intersectionWith(results1, results2, _.isEqual)
let finalResults = /* pass combinedResults into aggregation pipeline and execute it */
Something like the opposite of the $out operator, where I'm reading in a result set instead.
I'm using NestJS and Mongoose if that helps.