# https://www.cnblogs.com/cangqinglang/p/14858131.html
public function index(){
// 方法1 您可以使用 $toObjectId聚合实现此目的,该聚合仅将字符串ID转换为mongoose objectId
// https://blog.csdn.net/az9996/article/details/110141289
// 多表查询 https://blog.csdn.net/eric_to/article/details/90093806
$pipeline = [
['$addFields'=>['company_id'=>['$toObjectId'=>'$company_id']]],
['$match'=>['$expr'=>['$eq'=>["\$company_id", "$\$company_Id"]]]],
];
$pipelines = [
// ['$match'=>["_id"=>new \MongoDB\BSON\ObjectId("5cb7e580a6c98abc468b458b")]],
// ['$match'=>["name"=>"xxx限公司"]],
[
'$lookup'=>[
'from'=>'oa_company_member_pay',
"let"=>["company_Id"=>"\$_id" ],
"pipeline"=>$pipeline,
'as'=>'company_pay'
],
]
];
// php 执行 产生了 2571 条数据 但是 用 db.runCommand() 执行 打印的 sql 语句 只有 101 条记录
dump(Db::connect('mongo')->name('oa_company')->cmd([
'aggregate'=>'oa_company',
'pipeline'=>$pipelines,
'explain'=>false,
]));
dump(Db::connect('mongo')->getLastSql());
// 以下 mongodb sql 在 数据库 查询 通过 有 2571 条记录
// db.getCollection("oa_company").aggregate([
// {
// "$lookup": {
// "from": "oa_company_member_pay",
// "let": { "company_Id": "$_id" },
// "pipeline": [
// { '$addFields': { "company_id": { "$toObjectId": "$company_id" }}},
// { "$match": { "$expr": { "$eq": [ "$company_id", "$$company_Id" ] } } }
// ],
// "as": "company_member_pay"
// }
// }
// ])
// 以下 mongodb sql 在 数据库 查询 通过 只有 101 条记录
// db.runCommand({
// "aggregate":"oa_company",
// "pipeline":[
// {
// "$lookup":{
// "from": "oa_company_member_pay",
// "let": { "company_Id": "$_id" },
// "pipeline": [
// { '$addFields': { "company_id": { "$toObjectId": "$company_id" }}},
// { "$match": { "$expr": { "$eq": [ "$company_id", "$$company_Id" ] } } }
// ],
// "as": "company_member_pay"
// }
// }
// ],
// "explain":false
// })
// 方法2 或者使用$toString聚合 可以通过 2571
// db.getCollection('oa_company').aggregate([
// { "$addFields": { "companyidStr": { "$toString": "$_id" }}},
// { "$lookup": {
// "from": "oa_company_member_pay",
// "localField": "companyidStr",
// "foreignField": "company_id",
// "as": "pay"
// }}
// ])
/*
$pipelines = [
// ['$match'=>["_id"=>new \MongoDB\BSON\ObjectId("5cb7e580a6c98abc468b458b")]],
// ['$match'=>["name"=>"xxx有限公司"]],
['$addFields'=>['companyidStr'=>['$toString'=>'$_id']]],
[
'$lookup'=>[
'from'=>'oa_company_member_pay',
'localField'=>'companyidStr',
'foreignField'=>'company_id',
'as'=>'company_pay'
],
]
];
// php 执行 产生了 2571 条数据 但是 用 db.runCommand() 执行 打印的 sql 语句 只有 101 条记录
dump(Db::connect('mongo')->name('oa_company')->cmd([
'aggregate'=>'oa_company',
'pipeline'=>$pipelines,
'explain'=>false,
]));
dump(Db::connect('mongo')->getLastSql());
// db.cmd({"aggregate":"oa_company","pipeline":[{"$addFields":{"companyidStr":{"$toString":"$_id"}}},{"$lookup":{"from":"oa_company_member_pay","localField":"companyidStr","foreignField":"company_id","as":"company_pay"}}],"explain":false});
// 下面 执行 不可以 只有 101 条数据
db.runCommand({"aggregate":"oa_company","pipeline":[{"$addFields":{"companyidStr":{"$toString":"$_id"}}},{"$lookup":{"from":"oa_company_member_pay","localField":"companyidStr","foreignField":"company_id","as":"company_pay"}}],"explain":false});
*/
}