map()Mongo db Query - jenssegers / laravel-mongodb

时间:2016-02-25 12:30:39

标签: php mongodb laravel-4 jenssegers-mongodb

请参阅我的数据库查询:

db.comments
    .find({})
    .forEach(function(doc) { 
                        doc.comments.map(function(c) {
                                if (c.active == 1) {
                                    c.status = 0;
                                 }
                        }); 
                        db.comments.update(
                                      {_id: doc._id}, 
                                      {$set: {comments: doc.comments}});
     });

我需要用jenssegers / laravel-mongodb写。请帮助我如果有人有想法解决这个问题

1 个答案:

答案 0 :(得分:1)

class Comment extends Eloquent {}

$comments = Comment::all();
foreach ($comments as $c) {
    $c->comments = array_map(function (&$com) {
        if ($com->active == 1) $com->status = 0;
    }, $c->comments);
    $c->save();
}