1

I am a newbie in Laravel MongoDB I would like to mention a problem here, outline of my Mongo document should be like

{
     _id:****,
     subscriptions: [{list_id: "14Q3"},
        {list_id: "153"}],
     offers: [ { targetURL: "www.qwerty.com", title: "25% discount" }, 
    { targetURL: "www.abcd.com", title: "55% discount" } ],
}

I have used the following code to insert a list id (was successful).

DB::connection('mongodb')->collection('subscribers')->insert(array(
                    '_id' => $subscriber->device_id,
                    'subscriptions' => array('list_id' => $subscriber->list1_id),
                     ));

But when I try to push another list_id as next object it is showing error. I used the following code for pushing

DB::connection('mongodb')->collection('subscribers')
->push('subscriptions', array('list_id' => $subscriber->list1_id));

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....

My controller is is given below, I create new collection for each subscriber. Problem is second 'if' statement

public function store() {

        $newsubscriber = Input::json(); 
        //DB::connection('mongodb')->collection($newsubscriber->get('device_id'))->delete();
        $result = Subscriber::where('list1_id',$newsubscriber->get('list_id'))->where('device_id',$newsubscriber->get('device_id'))->get();
        if (!$result->isEmpty()) {
        return "You are already a subscriber of this List";
    }
    else{
        $result1 = Subscriber::where('device_id',$newsubscriber->get('device_id'))->get();
        $subscriber = new Subscriber();
        $subscriber->list1_id = $newsubscriber->get('list_id');
        $subscriber->device_id = $newsubscriber->get('device_id');
        $subscriber->subtype = 1;
        $subscriber->save();

        if (!$result1->isEmpty()) {
            DB::connection('mongodb')->collection($subscriber->device_id)->push('subscriptions', array('list_id' => $subscriber->list1_id));
            return "Subscribed successfully 1";
        }
        else{
            DB::connection('mongodb')->collection($subscriber->device_id)->insert(array('_id' => $subscriber->device_id,'subscriptions' => array('list_id' => $subscriber->list1_id),

        ));

            return "Subscribed successfully 2";
        }

    }
    } 

First i have used following API

curl -H "Content-Type: application/json" -d '{"list_id":"2","device_id":"987654321"}' http://localhost/lemmeknw/public/index.php/api/v1/subscribe

This returned "Subscribed successfully 2"

but when I used API for second time

curl -H "Content-Type: application/json" -d '{"list_id":"1","device_id":"987654321"}' http://localhost/lemmeknw/public/index.php/api/v1/subscribe

There was error "Something went wrong"

Nithil George
  • 275
  • 7
  • 25

1 Answers1

2

I don't know much about Laravel, but what I'm seeing here:

DB::connection('mongodb')->collection('subscribers')->insert(array(
               '_id' => $subscriber->device_id,
               'subscriptions' => array('list_id' => $subscriber->list1_id),
              ));

Looks like you are creating subscriptions as an object {list_id: <subscriber-list1_id>} instead of an array of one object [{list_id: <subscriber-list1_id>}]. So when you try to use the push operation

DB::connection('mongodb')->collection('subscribers')
  ->push('subscriptions', array('list_id' => $subscriber->list1_id));

that is an attempt to push to an object, not an array. My guess is that you will need to modify your insert to be

DB::connection('mongodb')->collection('subscribers')->insert(array(
               '_id' => $subscriber->device_id,
               'subscriptions' => array(array('list_id' => $subscriber->list1_id)),
              ));
NoOutlet
  • 1,949
  • 1
  • 14
  • 22
  • do you know where can we find the laravel MongoDB collections stored? @NoOutlet – Nithil George Jan 31 '15 at 10:27
  • I'm afraid I don't really understand the question you're asking. Do you mean what database and collection? It looks like from `DB::connection('mongodb')->collection('subscribers')` that it would be in the database set up in the `mongodb` connection information which [looked like this](http://stackoverflow.com/questions/25339592/eloquent-outside-of-laravel-with-jenssegers-laravel-mongodb-multiple-db-connecti) and the collection `subscribers`? – NoOutlet Jan 31 '15 at 14:24
  • Sorry if I have confused you. My question was, in which directory MongoDB collections are been stored. I found them in /var/lib/mongodb. Your helping mentality is appreciated @NoOutlet – Nithil George Feb 02 '15 at 04:16