MongoDB似乎不想推送到数组 [英] MongoDB does not seem to want to push to array

查看:89
本文介绍了MongoDB似乎不想推送到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个在线购物车,并且在将商品添加到购物车时遇到了很大的问题

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.

请提供任何建议.

推荐答案

首次插入时,items字段不是数组(可能是字符串).

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

根据 mongodb $ push doc :

如果$ push语句中指定的字段,则操作将失败 不是数组.

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

将插入操作更改为:

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

然后运行更新查询.

这篇关于MongoDB似乎不想推送到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆