RussellHarrower
RussellHarrower

Reputation: 6820

MongoDB $addToSet not writing to database

I am not sure why but I am having a really hard time getting the $addToSet working for my sub array items.

This is what it should look like:

{
  "items": [
    {
      "id": "510bca8138fc5d6e38000000",
      "quantity": "1"
    },
    {
      "id": "51011a8138fc5d6348000000",
      "quantity": "1"
    }
  ],
  "session": "1359948849.291898629576",
  "status": "cart"
}

However, it seems to only allow the first:

{
  "items": [
    {
      "id": "510bca8138fc5d6e38000000",
      "quantity": "1"
    }
  ],
  "session": "1359948849.291898629576",
  "status": "cart"
}

and it just won't insert another sub array.

My code:

$document = $collection->findOne(array('session' => $_SESSION["redi-Shop"]));
    
        //print_r($document);
        if (null !== $document) {
            $collection->update(
                array('session' => $_SESSION["redi-Shop"]),
                array(
                    '$addToSet' => array(
                        'items' => $_POST['item']
                    ),
                    
            ));
            print_r($_POST['item']);
        }
        else
        {
            $collection->insert(
                array('session' => $_SESSION["redi-Shop"],
                'status' => "cart",
                    'items' => $_POST['item'])
                );
        }
        

Upvotes: 0

Views: 2378

Answers (1)

RussellHarrower
RussellHarrower

Reputation: 6820

I changed the $addToSet to $Push and it works fine

Upvotes: 1

Related Questions