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"

0

1 Answer 1

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)),
              ));
Sign up to request clarification or add additional context in comments.

3 Comments

do you know where can we find the laravel MongoDB collections stored? @NoOutlet
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 and the collection subscribers?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.