Jessneggers / Laravel MongoDB whereRaw lookup not working

1.1k Views Asked by At

I migrated my database from Sql Server to MongoDB

I want to Join existing customer Table with contact Table .

Customer have multiple contacts . I tried whereRaw lookup

customer collection

{
    "_id": 77,
    "custid": 93
}

Contact Collection

{"_id":77,"contactid":77,"custid":93,"firstname":"Christy ","lastname":"Lambright" }

{"_id":79,"contactid":79, "custid":93,"firstname":"Marlys ","lastname":"Barry" }

Customer Modal

class custt extends Model
{
    use Notifiable;

    protected $primaryKey = 'id';

}

Contact Modal

class contact extends Model
{
    use Notifiable;

    protected $primaryKey = 'id';

In Controller

$cnt = DB::collection("custts")->raw(function($collection)
             {
                 $more_where = [];
                    $more_where[]['$lookup'] = array(
                      'from' => 'contacts',
                      'localField' => 'custid',
                      'foreignField' => 'custid',
                      'as' => 'country',
                    );


                 return $collection->aggregate($more_where);
             });

  Error comes -- 

Empty Results

I tried Lots of options for hasMany and belongstoMany . Not working ...

please suggest

1

There are 1 best solutions below

0
On

ok , finally found it working

source - https://github.com/jenssegers/laravel-mongodb/issues/841

$cnt = custt::raw(function($collection)
            {
                return $collection->aggregate(
                    [[
                        '$lookup' => [
                            'as'=>'info',
                            'from'=>'contacts',
                            'foreignField'=>'custid',
                            'localField'=>'custid'
                        ]
                    ]]
                );
            });