Php Laravel MongoDB:我们能用它拉一个特定的子文档吗';谁的身份证?
这是我的收藏Php Laravel MongoDB:我们能用它拉一个特定的子文档吗';谁的身份证?,php,mongodb,laravel,laravel-4,Php,Mongodb,Laravel,Laravel 4,这是我的收藏 { "_id": "987654322", "subscriptions": [ { "list_id": "13" }, { "list_id": "12" } ], "offers": [ { "campaign_id": 47, "title": "MasalaBox A
{
"_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中执行吗?可以使用Jensegers软件包执行。但是,如果要对数据进行深度嵌套,则需要更改集合中文档的结构
"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');
这将删除特定的子文档。尽管我建议不要深入嵌套,因为这样会给搜索带来麻烦
如果你更深入一点,可能最好创建一个新的订阅模型,并使用belongtomany-belongtomany关系将两者连接起来
致以最良好的祝愿
Bdschr