首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel MongoDB:我们能用它的id提取一个特定的子文档吗?

Laravel MongoDB:我们能用它的id提取一个特定的子文档吗?
EN

Stack Overflow用户
提问于 2015-02-05 17:29:03
回答 1查看 1.1K关注 0票数 1

这是我的收藏订阅者

代码语言:javascript
运行
复制
{
    "_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个文档

代码语言:javascript
运行
复制
 {
            "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"
        }

我过去常常按如下所示进行推送

代码语言:javascript
运行
复制
$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中实现?

代码语言:javascript
运行
复制
  dbh.subscription.update({"_id": "987654322"}, {"$unset":{"offers.campaign_id":47}},False,False)

我已经尝试了几天来解决这个问题,但还是弄不明白。可以在Laravel MongoDB中执行吗?

EN

Stack Overflow用户

发布于 2016-01-20 18:09:09

这可以通过Jenseggers包来完成。但是,如果您是深度嵌套数据,则需要更改集合中文档的结构。

代码语言:javascript
运行
复制
"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"
    }
}

然后查询

代码语言:javascript
运行
复制
Subscription::where('offers.47', 'exists', true)->unset('offers.47');

这应该会删除特定的子文档。虽然我建议不要深入嵌套,因为这将是搜索的麻烦。

如果深入研究,最好创建一个新的订阅模型,并使用从属关系-从属关系将两者连接起来。

诚挚的问候,

Bdschr

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28340383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档