Say I have two collections: orders and items. Orders can have 1 to many items, which orders has an array of item ids.
I have a page that shows all orders. I want to enable a search bar that allows users to search orders. Since my items are not embedded into the orders, I have to do a $lookup for the items.
I want the user to be able to search as if the items where embedded into the orders document.
Sample data:
Orders:
[
{
_id: "1",
buyer: "John doe",
itemIds: ["1", "2"],
},
{
_id: "2",
buyer: "Jane doe",
itemIds: ["3"],
}
]
Items:
[
{
_id: "1",
name: "Shoe",
description: "Red shoe"
},
{
_id: "2",
name: "Pants",
description: "Blue Jeans"
},
{
_id: "3",
name: "Shirt",
description: "White Tshirt"
},
]
If the user searches “shoe”, it should return order 1
If the user searches “jane”, it should return order 2
Im trying to follow the $search $lookup documentation but don’t understand how to make it work for my use case: https://www.mongodb.com/docs/atlas/atlas-search/tutorial/lookup-with-search/
- Use
$search
stage in the sub-pipeline to search for customer accounts thatmust
have purchased bothCurrencyService
andInvestmentStock
with preference for an order limit between5000
to10000
.