-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Can't get pull operations quite right #2070
-
I can't get my pull operation quite right, can someone help me please?
I am trying to remove an element in a nested array inside mongo in my laravel project. I tried many ways to do it but after checking nothing was changed in my array.
I tried multiple way with no luck
$removeItem = Carts::query()
->where('userUID', '=', $user->UID)
->where('cart', '=', 'default')
->where('items.item', '=', $item['item'])
->pull(
'items',
'items.$',
);
i tried this as well:
$removeItem = Carts::query()
->where('userUID', '=', $user->UID)
->where('cart', '=', 'default')
->where('items.item', '=', $item['item'])
->pull(
'items',
[
'item' => 'items.$.item',
'location' => 'items.$.location',
'price' => 'items.$.price',
'quantity' => 'items.$.quantity',
'status' => 'items.$.status',
'added_on' => 'items.$.added_on',
'updated_on' => 'items.$.updated_on',
'deleted_on' => 'items.$.deleted_on',
]
);
My document structure can be seeing here:
I also posted on SO just in case:
https://stackoverflow.com/questions/63085367/pull-an-item-from-a-mongodb-array-using-jessengers-laravel
Beta Was this translation helpful? Give feedback.
All reactions
I was able to fix it by adding the variable name to it and also because i am removing an index, going in reverse like so:
for ($i=count($userCartBis['items']) - 1; $i >= 0; $i--) {
//for now remove each element from the cart and put it in the orders section, if it's active
if ($userCartBis['items'][$i]['status'] == 'active') {
//this one we can process
//add it to the orders table
$updateOrder = $order
->push(
'products',
[
'location' => $userCartBis['items'][$i]['location'],
'quantity' => $userCartBis['items'][$i]['quantity'],
'price' => $userCartBis['items'][$i]['price'],
'product' => $userCartBis['i...
Replies: 2 comments
-
i confirm the same problem pull method not work for me.
- jenssegers/laravel-mongodb v.dev
- Mongo version 4.2.5
My data
#attributes: array:23 [
"_id" => MongoDB\BSON\ObjectId {#587
+"oid": "5f19e43f5180dd220976066f"
}
"total_info" => array:2 [
0 => array:3 [
"description" => "ส่วนลด 50 บ. ซื้อขั้นต่ํา 99 บ."
"value" => -50
"campaign_id" => "5f1083f9ac29ac79c7529af4"
]
1 => array:3 [
"description" => "ส่วนลด 50 บ. ซื้อขั้นต่ํา 99 บ."
"value" => -50
"campaign_id" => "5f1083f9ac29ac79c7529af4"
]
]
$order->pull('total_info', [
'campaign_id' => '5f1083f9ac29ac79c7529af4'
]);
Query Log
db.merchant_order.UpdateMany({"$and":[{"_id":"5f19e43f5180dd220976066f"},{"deleted_at":null}]},{"$pull":{"total_info":{"campaign_id":"5f1083f9ac29ac79c7529af4"}}},{"multiple":true})
Thanks
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
I was able to fix it by adding the variable name to it and also because i am removing an index, going in reverse like so:
for ($i=count($userCartBis['items']) - 1; $i >= 0; $i--) {
//for now remove each element from the cart and put it in the orders section, if it's active
if ($userCartBis['items'][$i]['status'] == 'active') {
//this one we can process
//add it to the orders table
$updateOrder = $order
->push(
'products',
[
'location' => $userCartBis['items'][$i]['location'],
'quantity' => $userCartBis['items'][$i]['quantity'],
'price' => $userCartBis['items'][$i]['price'],
'product' => $userCartBis['items'][$i]['item'],
]
);
//remove it from the cart table
$removeItem = $userCart
->pull(
'items',
$userCartBis['items'][$i],
);
} else if ($userCartBis['items'][$i]['status'] == 'deleted') {
//it is already deleted
//do nothing for now
}
}
Beta Was this translation helpful? Give feedback.