Cannot query with an array position
nicolasvahidzein opened this issue · comments
Hello
I'm using jenssegers/mongodb: ^3.9,
I know i am using an older version but it's impossible for me to update now as i am in production and i have no time to upgrade right now.
I have this query below
$stockedProducts = ProductsDetails::query()
->where('seller_uid', '=', $seller->UID)
->where('locations.location', '=', $locationUid)
->where('locations.quantity_stock', '=', 0);
but it is returning items that do not have a 0 stock because i am not able to isolate the location in the query. If a product has any location at all that carries a 0 stock quantity it will be included in the query results. What i would need is the query below but that one is broken and returns no results at all.
My desired query (notice the .$. in the 4th line):
$stockedProducts = ProductsDetails::query()
->where('seller_uid', '=', $seller->UID)
->where('locations.location', '=', $locationUid)
->where('locations.$.quantity_stock', '=', 0);
But this doesn't work. It works when i try to update the data but not in the initial query.
I need a way to tell the query that once it found the proper location in line 3 to make sure that the stock for only that location must be 0.
How could i accomplish this like i want?
Thank you so much for your assistance.
Hello @nicolasvahidzein, you can use the $elemMatch
operator.
$stockedProducts = ProductsDetails::query()
->where('seller_uid', '=', $seller->UID)
->where('locations', 'elemmatch', [
'location' => $locationUid,
'quantity_stock' => 0,
]);
Thank you so much @GromNaN. You are very kind. Let me test and revert back.
Cheers.