Return results from within arrays
I have a document that looks like this:
{
shoppingList: [ { name: "red hat"}, { name: "green shoes"} ]
}
I would like to query $search for "green" and return {name: "green shoes"} instead of returning the entire document.
10
votes

-
Ethan commented
For those reading, there is an operator to search within the objects of arrays, however that will merely tell you that the string exists within the array of objects. It's called embeddedDocuments. To discover which object within the array matches a query, other methods like a materialized view may be needed.
-
Maria commented
I also need to be able to search within an array of documents and retrieve one document out of the array that matches my query params.