Hello All:
I am running an aggregation with a $Search (atlas search) as follows:
[
{
$search: {
index: "indexDeos",
// Replace with the name of your search index
text: {
query: "trumpet",
// Use the text from the Leo
path: "text",
// Path to the text field in the Deos collection
fuzzy: {}, // Enables fuzzy matching
},
},
},
]
Upon running this I get some results. However if I run a $lookup on the resultant data as follows, I get null user data.
[
{
$search: {
index: "indexDeos",
// Replace with the name of your search index
text: {
query: "trumpet",
// Use the text from the Leo
path: "text",
// Path to the text field in the Deos collection
fuzzy: {}, // Enables fuzzy matching
},
},
},
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user",
},
},
]
Here is the resultant data, note that the user is null.
_id
677e2193830fb96c3c90e62d
createdAt
2025-01-08T06:56:19.619+00:00
status
Array (1)
text
"Trumpat"
userId
677e0ff7674b72e983cb87e4
user
Array (empty)```
In the above case I am searching through a collection and creating a resultant set of data which I am joing with my user collection on userId.
However if i run the $lookup without applying it to the resultant data from the $search as follows, it works and gives me the user data.
[
{
$lookup: {
from: “users”,
localField: “userId”,
foreignField: “_id”,
as: “user”,
},
},
]```
Here is the resultant data:
createdAt
2025-01-07T10:42:31.061+00:00
status
Array (1)
text
"This is the first deo in the new system"
userId
64f56b4134b25512c4d86e2d
user
Array (1)
0
Object
_id
677d0517bc7c5e17bfc25eac
My question is why is the $lookup (I am using it to join to my users table) not working with the data returned by $search, when otherwise the $lookup is working. Is the data returned by $search in a previous aggregation pipeline stage, special in some way?
If I were to put the $lookup before the $search or inside it as recommended here
https://www.mongodb.com/docs/atlas/atlas-search/tutorial/lookup-with-search/
then it would first join my entire collection and then do a $search on the entire joined collection, which would be operationally inefficient.
Why is it not possible to simply do the $lookup after the $search in the aggregation pipeline? Afterall that is what the pipeline is for right? To be able to perform further actions on data returned by a previous stage.
Please help Mongo team. Both me and Chatgpt are out of ideas on this one!!!