【问题标题】:Jessneggers / Laravel MongoDB whereRaw lookup not workingJessneggers / Laravel MongoDB whereRaw 查找不起作用
【发布时间】:2020-06-03 09:57:17
【问题描述】:

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

我想加入现有的客户表和联系人表。

客户有多个联系人。我试过 whereRaw 查找

客户收集

{
    "_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 尝试了很多选项。不工作...

请推荐

【问题讨论】:

    标签: database mongodb laravel-mongodb


    【解决方案1】:

    好的,终于可以正常使用了

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2019-12-05
      • 2021-03-15
      • 2017-05-21
      • 1970-01-01
      • 2018-11-10
      相关资源
      最近更新 更多