database - Jessneggers / Laravel MongoDB whereRaw 查找不起作用
问题描述
我将我的数据库从 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
。不工作...
请建议
解决方案
好的,终于发现它工作了
来源 - 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'
]
]]
);
});
推荐阅读
- python - 尝试使用 Lambda 函数并获取 NAN
- mysql - 弧度函数内的 Mysql 查询不起作用。为什么?
- python - Python3.6: pool.map throws -- AssertionError: daemonic processes are not allowed to have children
- javascript - 如何访问使用 supertiler 库创建的群集矢量源的 clusterProperties?
- ansible - Ansible:使用 yaml 库存的两个主机组的交集
- r - 需要帮助根据列值对数据进行子集化
- c# - 用于 OCR 响应的 C# JSON 去中心化
- django - 使用 django_crontab 包调度管理命令
- powershell - Powershell,转义正则表达式转义方法中不存在的字符串变量中的特定字符
- c++ - 如何从 std::cin 读取到 istringstream