How to create a blob in MongoDB using laravel

怎甘沉沦 提交于 2019-12-12 02:27:03

问题


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)); 


来源:https://stackoverflow.com/questions/27841017/how-to-create-a-blob-in-mongodb-using-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!