How to create a blob in MongoDB using laravel

2019-09-10 09:48发布

问题:

I am using Laravel4 PHP Framework, my web application has to handle JSON format APIs.

First part of My problem is, each time when i get a subscription, deviceID and listID (where list ID is the the list which was subscribed by the device having deviceID) is received by the web application. For each deviceID received we have to create a blob having deviceID like

{
    deviceID:........,
    subscription:[{listID:1},{listID:2}.......]
}

when the same device subscribe multiple list i have to update subscription, similerly when new device subscribes, a new document has to be created as above.

Second part of my problem is our web will create a offer campaigns for each Lists.Each campaign would have fiels like Title, Icon, Image,Detailed description etc. when a campaign is ON, this campaign has to inserted in to offers array of above document as an object Like:

{
    deviceID:........,
    subscription:[{listID:1},{listID:2}.......],
    offers [{campaign1}]
}

The campaign would have many fields.

I have tried

db.subscribers.insert(
{
    _id:98745334,
    subscriptions: [{
        list_id: "14Q3"}, {list_id: "8989"}
    ],
    offers: [ { title: "50% off", qty: 25 }, { title: "20% off", qty: 50 } ],
})

This worked. But, I couldn't insert or retrieve or updaate any data using Laravel4.

I am using MongoDB and Laravel4

Sorry for bad english Please help me

回答1:

As I have Succeeded in inserting as following

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

For pushing into Subscriptions array I used following code

Subscription::where('_id',$subscriber->device_id)->push('subscriptions', array('list_id' => $subscriber->list1_id));