【问题标题】:How to implement the following Mongo pseudocode in Laravel?如何在 Laravel 中实现以下 Mongo 伪代码?
【发布时间】:2018-12-24 05:34:40
【问题描述】:

我要实现:

db.lawcases.find().snapshot().forEach(
    function (elem) {
        db.lawcases.update(
            {
                _id: elem._id
            },
            {
                $set: {
                    name: elem.firstname + ' ' + elem.lastname
                }
            }
        );
    }
);

我正在使用 Laravael MongoDB 包,它允许我按照 https://github.com/jenssegers/Laravel-MongoDB#raw-expressions 中的说明进行原始查询。

到目前为止:

$lawCases =  \DB::connection('mongodb')->collection('lawcases')->raw(function($collection)
        {
            return $collection->find();
        });



        foreach($lawCases as $case){
                    //DO SOMETHING

            }

但我需要一些帮助,因为我不知道如何坚持更改。我觉得我做得不对。

【问题讨论】:

    标签: php mongodb laravel mongodb-query


    【解决方案1】:

    像使用 Laravel 的数据库一样使用它:

    <?php
    
    // get the cases first
    $lawCases = \DB::connection('mongodb')
        ->collection('lawcases')
        ->get();
    
    // update each record
    foreach($lawCases as $case)
    {
        \DB::connection('mongodb')
        ->collection('lawcases')
        ->where([
            "_id" =>  $case["_id"]
        ])
        ->update([
            "name" => $case["firstname"] . " " $case["lastname"]
        ]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 2015-09-22
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多