I will query data so check last index of array “stock.updates” by value { “status”: “pending” }
then result I want to show data only document { “productId”: “2”… }
The caveat about this solution is that no index can be used because it $match on a computed value. I do not even know if it is possible to come up with a query that can use an index with this schema. However by having a copy of the last status in a separate field, then the query is trivial and can use an index. Documents like:
I successfully resolved the issue.
Initially, I attempted to find a solution by querying the find() function, but I couldn’t locate information on accessing the last array element using $last.
I appreciate your assistance in guiding me through the solution.