title: How to insert an object into MongoDB document using Laravel 4
body: <p>I am a newbie in Laravel MongoDB I would like to mention a problem here, outline of my Mongo document should be like</p>
  
  <pre><code>{
       _id:****,
       subscriptions: [{list_id: "14Q3"},
          {list_id: "153"}],
       offers: [ { targetURL: "www.qwerty.com", title: "25% discount" }, 
      { targetURL: "www.abcd.com", title: "55% discount" } ],
  }
  </code></pre>
  
  <p>I have used the following code to insert a list id (was successful).</p>
  
  <pre><code>DB::connection('mongodb')-&gt;collection('subscribers')-&gt;insert(array(
                      '_id' =&gt; $subscriber-&gt;device_id,
                      'subscriptions' =&gt; array('list_id' =&gt; $subscriber-&gt;list1_id),
                       ));
  </code></pre>
  
  <p>But when I try to push another list_id as next object it is showing error. I used the following code for pushing</p>
  
  <pre><code>DB::connection('mongodb')-&gt;collection('subscribers')
  -&gt;push('subscriptions', array('list_id' =&gt; $subscriber-&gt;list1_id));
  </code></pre>
  
  <p>I don't know whether my code is right or wrong. I need to store the data as the outline given above. This is My actual problem. Please correct me....</p>
  
  <p>My controller is is given below, I create new collection for each subscriber. Problem is second 'if' statement</p>
  
  <pre><code>public function store() {
  
          $newsubscriber = Input::json(); 
          //DB::connection('mongodb')-&gt;collection($newsubscriber-&gt;get('device_id'))-&gt;delete();
          $result = Subscriber::where('list1_id',$newsubscriber-&gt;get('list_id'))-&gt;where('device_id',$newsubscriber-&gt;get('device_id'))-&gt;get();
          if (!$result-&gt;isEmpty()) {
          return "You are already a subscriber of this List";
      }
      else{
          $result1 = Subscriber::where('device_id',$newsubscriber-&gt;get('device_id'))-&gt;get();
          $subscriber = new Subscriber();
          $subscriber-&gt;list1_id = $newsubscriber-&gt;get('list_id');
          $subscriber-&gt;device_id = $newsubscriber-&gt;get('device_id');
          $subscriber-&gt;subtype = 1;
          $subscriber-&gt;save();
  
          if (!$result1-&gt;isEmpty()) {
              DB::connection('mongodb')-&gt;collection($subscriber-&gt;device_id)-&gt;push('subscriptions', array('list_id' =&gt; $subscriber-&gt;list1_id));
              return "Subscribed successfully 1";
          }
          else{
              DB::connection('mongodb')-&gt;collection($subscriber-&gt;device_id)-&gt;insert(array('_id' =&gt; $subscriber-&gt;device_id,'subscriptions' =&gt; array('list_id' =&gt; $subscriber-&gt;list1_id),
  
          ));
  
              return "Subscribed successfully 2";
          }
  
      }
      } 
  </code></pre>
  
  <p>First i have used following API</p>
  
  <p><code>curl -H "Content-Type: application/json" -d '{"list_id":"2","device_id":"987654321"}'</code> <a href="http://localhost/lemmeknw/public/index.php/api/v1/subscribe" rel="nofollow">http://localhost/lemmeknw/public/index.php/api/v1/subscribe</a></p>
  
  <p>This returned "Subscribed successfully 2"</p>
  
  <p>but when I used API for second time</p>
  
  <p><code>curl -H "Content-Type: application/json" -d '{"list_id":"1","device_id":"987654321"}'</code> <a href="http://localhost/lemmeknw/public/index.php/api/v1/subscribe" rel="nofollow">http://localhost/lemmeknw/public/index.php/api/v1/subscribe</a></p>
  
  <p>There was error "Something went wrong"</p>