I am learning mongoDB and trying converting SQL query code as a process and way of being familiarized with the language.
I have the following query in SQL:
$where = !empty($conta)? " WHERE marker NOT ILIKE '%' || LPAD({$conta}::text, 5, '0') || '%' " : "";
WITH RECURSIVE cte AS (
SELECT
LPAD(id::text, 5, '0') AS marker,
id,
plano_conta_id,
nome::text,
' .. ' AS buffer
FROM
plano_contas t
WHERE
plano_conta_id IS NULL
UNION ALL
SELECT
t2.marker || ':' || LPAD(t1.plano_conta_id::text, 5, '0') || ':' || LPAD(t1.id::text, 5, '0') AS marker,
t1.id,
t1.plano_conta_id,
t2.buffer || t1.nome,
t2.buffer || ' .. '
FROM
plano_contas t1
INNER JOIN cte t2 ON t1.plano_conta_id = t2.id
)
SELECT marker, id, nome FROM cte {$where} ORDER BY marker;
I do not know what I am doing wrong. Here is my mongodb php code
$firstMatch = array('$match' => array( 'plano_conta_id' => array('$exists' => false)));
$marker = new MongoDB\BSON\Regex("^marker.*$", 'i');
$or = array('$or' => array('$not' => $marker, lpad(array('$arrayElemAt' => array('$conta' => 'text',0),5,"0"))));
$secondMatch = array('$match' => array($or));
$thirdMatch = array('$match' => array());
$if = array(array("$conta" => array('$exists' => false)));
$cond = array($if, $secondMatch, $thirdMatch );
$firstGroup = array('$group' => array('marker' => lpad(array('$arrayElemAt' => array('id' => 'text' ,0),5,"0") ,'..', )),
'id_conta' => 'id_conta',
'plano_conta_id' => 'plano_conta_id',
'nome' => 'text',
'..' => 'buffer'
);
$orGroup = array('$or' => array('t2.marker' => 't2.marker',
':' => ':',
lpad(array('$arrayElemAt' => array('t1.plano_conta_id' => 'text' ,0),5,"0")),
':' => ':',
'marker' => lpad(array('$arrayElemAt' => array('t1.id' => 'text' ,0),5,"0"))
));
$orGroup2 = array('$or' => array('t2.buffer' => 't2.buffer',
't1.nome' => 't1.nome'
));
$orGroup3 = array('$or' => array('t2.buffer' => 't2.buffer',
':' => ':'
));
$secondGroup = array('$group' => array($orGroup,
't1.id' => 't1.id',
't1.plano_conta_id' => 't1.plano_conta_id',
$orGroup2,
$orGroup3
));
$thirdGroup = array('$group' => array('marker' => 'marker',
'id_conta' => 'id_conta',
'nome' => 'nome'
));
$lookup = array('$lookup' => array('from' => "plano_conta_id",
'localField' => "t1.plano_conta_id",
'foreignField' => "t2.id_conta",
'as' => "cte"
));
$sort = array('marker' => 'marker');
$firstProject = array('$project' => $firstGroup);
$secondProject = array('$project' => $secondGroup);
$thirdProject = array('$project' => $thirdGroup);
$graphLookup = array('$graphLookup' => array('from' => 'bancoTeste.plano_contas',
'startWith' => "id_conta",
'connectFromField' => "plano_conta_id",
'connectToField' => "id_conta",
'as' => "cte" ),
$firstMatch, $firstProject,
'$unionWith' => array($lookup,
$secondProject
),$cond, $thirdProject, $sort
);
$conexaoMongo = new MongoDB\Client('mongodb://localhost:27017');
$banco = $conexaoMongo->bancoTeste;
$cadastro = $banco->plano_contas;
$objects = $cadastro->aggregate([ $graphLookup ]);
I tried to code SQL query to mongodb, but I did not get succeeded. I searched for similar methods like lpads, but I think that what I found does not work for this code.
Here are some links through which I made structure comparisons: