Saswat
Saswat

Reputation: 12846

Fetch employee list along with number of task completed by each employee, in Laravel MongoDB

I am using Laravel MongoDB library (formerly know as Laravel Jenssegers MongoDB library).

This is my employees collection

{
    _id         : {$oid: 2ndkj3720eA83b24},
    name        : 'Employee1',
    status      : 'a',
    deleted_at  : null
}

This is my task collection:-

{
    _id              : {$oid: 893jfo2ok01398190},
    name             : 'Task 1',
    employees        : [
                         {$oid: ae2nvg6788eA83b24},
                         {$oid: be09gh56701398190},
                         {$oid: bf0e28bfi45202bc0}
                       ],
    complete_status  : 'c',
    status           : 'a',
    deleted_at       : null
}

The employees column in task collection is an array of object ids of employees.

This is how I fetch employee list:-

$employeeQuery  = Employee::where(array(
                                'status'          => Globals::SMALL_CHAR_ACTIVE,
                                'deleted_at'      => NULL
                            ));

if($employeeQuery->count() > 0) {
    $employeeListing = $employeeQuery->get()->toArray();
}

But now I have to fetch employee list along with number of completed tasks for each employee. Like this:-

{
    _id  : {$oid: 2ndkj3720eA83b24},
    name : 'Employee1',
    task_completed: 23
}

How can I do this?

Upvotes: 0

Views: 24

Answers (0)

Related Questions