MongoDB does not seem to want to push to array

不羁的心 提交于 2019-12-11 23:21:24

问题


I am making an online shopping cart, and I having huge issues pushing add to cart to items

 $collection->update(
                array('session' => $_SESSION["redi-Shop"],
                array('$push'=>
                array('items'=> $_POST["item"])
                )));

When the customer selects their first item to add to the cart it works fine

   $collection->insert(
   array('session' => $_SESSION["redi-Shop"],
   'status' => "cart",
   'items' =>$_POST['item']));

but after the first item is added it does not allow me to add any more.

Please any advice would be helpful.


回答1:


When you insert it the first time, the items field is not array (probably a string).

According to the mongodb $push doc :

The operation will fail if the field specified in the $push statement is not an array.

Change your insert operation to :

$collection->insert(
   array(
      'session' => $_SESSION["redi-Shop"],
      'status' => "cart",
      'items' => array($_POST['item'])
   ));

Then run your update query.



来源:https://stackoverflow.com/questions/14687113/mongodb-does-not-seem-to-want-to-push-to-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!