Reputation: 275
This is my collection subscribers
{
"_id": "987654322",
"subscriptions": [
{
"list_id": "13"
},
{
"list_id": "12"
}
],
"offers": [
{
"campaign_id": 47,
"title": "MasalaBox Awesome Sunday Offer",
"description": "50% off on ALL online purchases!!",
"thunbnailURL": "thumb/5m9Oav_images (4).jpg",
"detailImageURL": "img/BpWyBT_original_290019_9BoDnY6NTBGpQq84atetEsA5Z.jpg",
"detailedDescription": "Masalabox proudly presents the Awesome Sunday offer!! More info goes here. And some more here. Blah blah blah blah blah blah blah blah blah blah blah blah blah",
"targetURL": "http://www.altavista.com/",
"end_date": "2015-02-28"
}
]
}
All I want to do is: (1) search the collection for 'campaign_id' 47 in ALL subdocuments in field "offers". (2) If 'campaign_id'47 exists in a subdocument, I want to remove entire 47 document
{
"campaign_id": 47,
"title": "MasalaBox Awesome Sunday Offer",
"description": "50% off on ALL online purchases!!",
"thunbnailURL": "thumb/5m9Oav_images (4).jpg",
"detailImageURL": "img/BpWyBT_original_290019_9BoDnY6NTBGpQq84atetEsA5Z.jpg",
"detailedDescription": "Masalabox proudly presents the Awesome Sunday offer!! More info goes here. And some more here. Blah blah blah blah blah blah blah blah blah blah blah blah blah",
"targetURL": "http://www.altavista.com/",
"end_date": "2015-02-28"
}
I used to push as given below
$offer = [
'campaign_id' => $campaign->id,
'title' => $template->title,
'description' => $template->description,
'thunbnailURL' => $template->thunbnailURL,
'detailImageURL' => $template->detailImageURL,
'detailedDescription' => $template->detailedDescription,
'targetURL' => $template->targetURL,
'end_date' => $template->end_date,
];
$subscribers= Subscription::where('offers.campaign_id', $campaign->id)->get();
foreach ($subscribers as $subscriber) {
Subscription::where('_id',$subscriber->_id)->push('offers', array($offer));
}
I think the following code could solve my issue, but, how can it be implemented in Laravel?
dbh.subscription.update({"_id": "987654322"}, {"$unset":{"offers.campaign_id":47}},False,False)
I have been trying to solve this for few days and cannot figure it out. Is it possible to perform in Laravel MongoDB ?
Upvotes: 1
Views: 1381
Reputation: 31
It can be done with the Jenseggers package. But you will need to change the structure of the document in your collection if you're deep nesting data.
"offers": {
"47" : {
"title": "MasalaBox Awesome Sunday Offer",
"description": "50% off on ALL online purchases!!",
"thunbnailURL": "thumb/5m9Oav_images (4).jpg",
"detailImageURL": "img/BpWyBT_original_290019_9BoDnY6NTBGpQq84atetEsA5Z.jpg",
"detailedDescription": "Masalabox proudly presents the Awesome Sunday offer!! More info goes here. And some more here. Blah blah blah blah blah blah blah blah blah blah blah blah blah",
"targetURL": "http://www.altavista.com/",
"end_date": "2015-02-28"
}
}
and then the query
Subscription::where('offers.47', 'exists', true)->unset('offers.47');
This should delete the specific subdocument. Although I recommend not go to deep with the nesting because it will be troublesome with searching.
If you go deeper it's probably better to create a new Subscription model and use a belongstomany - belongstomany relationship to connect both.
Best Regards,
Bdschr
Upvotes: 1