Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Discussion options

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:

mongo_pull_doc

I also posted on SO just in case:
https://stackoverflow.com/questions/63085367/pull-an-item-from-a-mongodb-array-using-jessengers-laravel

You must be logged in to vote

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

Comment options

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

You must be logged in to vote
0 replies
Comment options

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
			}
			
		}
You must be logged in to vote
0 replies
Answer selected by divine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #2070 on September 02, 2020 05:32.

AltStyle によって変換されたページ (->オリジナル) /