Search code examples
phpmongodblaravel-4jenssegers-mongodb

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


Please see my database query:

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}});
     });

I need to write with jenssegers/laravel-mongodb. please help me If anyone has an idea to solve this


Solution

  • 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();
    }