Facing issue while reusing mongo connection

88 Views Asked by At

I have 'employees' collection

I'm creating new mongo connection by using following code

$mongoObject = DB::connection('mongodb')->collection('employees');

//fetch employee by employee id
$employee = $mongoObject->where('employee_id', $input['employee_id'])->first();

//Fetch all employees 
$employees = $mongoObject->get();

Now my problem is that I got first response properly but when I tried to fetch all employees by using same mongo connection it gives only one record. As per my understanding it's not flushing conditions after I use ->first() of eloquent.

Is there any way to reuse the same mongo connection by using eloquent methods?

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

After calling first() the limit is being set to 1 which is why get returns one record.

Following code works:-

$mongoObject = DB::connection('mongodb')->collection('employees');

//fetch employee by employee id
$employee = $mongoObject->where('employee_id', $input['employee_id'])->first();

//Fetch all employees 
$employees = $mongoObject->newQuery()->from('employees')->get();