Jessneggers / Laravel MongoDB原始查询不起作用

时间:2020-06-03 09:57:17

标签: database mongodb laravel-mongodb

我已将数据库从Sql Server迁移到MongoDB

我想将现有的客户表与联系表连接起来。

客户有多个联系人。我尝试在哪里进行原始查询

客户集合

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

联系人集合

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

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

客户模式

class custt extends Model
{
    use Notifiable;

    protected $primaryKey = 'id';

}

联系方式

class contact extends Model
{
    use Notifiable;

    protected $primaryKey = 'id';

在控制器中

$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 -- 

空结果

我尝试了hasManybelongstoMany的许多选项。不起作用...

请提出建议

1 个答案:

答案 0 :(得分:0)

好,终于发现它可以正常工作

源-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'
                        ]
                    ]]
                );
            });