这是我的收藏订阅者
{
"_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"
}
]
}我所要做的就是:(1)在"offers“字段中的所有子文档中搜索”campaign_id“47。(2)如果子文档中存在'campaign_id'47,我想删除整个47个文档
{
"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"
}我过去常常按如下所示进行推送
$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));
}我认为下面的代码可以解决我的问题,但是,它如何在Laravel中实现?
dbh.subscription.update({"_id": "987654322"}, {"$unset":{"offers.campaign_id":47}},False,False)我已经尝试了几天来解决这个问题,但还是弄不明白。可以在Laravel MongoDB中执行吗?
发布于 2016-01-20 18:09:09
这可以通过Jenseggers包来完成。但是,如果您是深度嵌套数据,则需要更改集合中文档的结构。
"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"
}
}然后查询
Subscription::where('offers.47', 'exists', true)->unset('offers.47');这应该会删除特定的子文档。虽然我建议不要深入嵌套,因为这将是搜索的麻烦。
如果深入研究,最好创建一个新的订阅模型,并使用从属关系-从属关系将两者连接起来。
诚挚的问候,
Bdschr
https://stackoverflow.com/questions/28340383
复制相似问题