我将我的数据库从 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 --
空结果
hasMany
我为and尝试了很多选项belongstoMany
。不工作...
请建议