我只有一个集合“详细信息”。它以不同的别名在查询中使用两次。由于 Mongo 没有别名,我认为 mapreduce 会给出结果。我也尝试过展开聚合,但它会在字段上展开,而不是在集合上展开。有关聚合或 mapreduce 的任何帮助。
Collection:
"details"
{
"user_id":1,
"lft":2
"rgt":5
},
{
"user_id":2,
"lft":1
"rgt":6
},
{
"user_id":3,
"lft":3
"rgt":4
}
SQL查询:
SELECT CONCAT( REPEAT('-', COUNT(parent.user_id) - 1), node.user_id)
AS user_id
FROM details AS node,
details AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.user_id
ORDER BY node.lft;
它应该输出:
1
-2
--3
我努力了:
$mongodb = Connections::get('default')->connection;
$details = Details::connection()->connection->command(array(
'aggregate' => 'details',
'pipeline' => array(
array('$project' => array(
'_id' => array(
'parent'=>array(
'puser_id'=>'$user_id',
'pleft'=>'$left',
'pright'=>'$right',
),
'node'=>array(
'nuser_id'=>'$user_id',
'nleft'=>'$left',
'nright'=>'$right',
)
),
),
'$group'=>array('_id'=>'$_id.parent.puser_id'),
'$match' => array(
'$_id.node.nleft'=>array('$gt'=>'$_id.parent.pleft'),
'$_id.node.nright'=>array('$gt'=>'$_id.parent.pright')
)
),
)
));
我被困在 $group 和 $match 上!