我正在使用PHP与MongoDB交互,最近我在5个集合上使用$lookup
,我的查询开始花费大约20-25秒来获得结果。我的所有馆藏都有不超过30-40个条目的测试数据。我也使用了分析,但我收到消息:"查询不记录(太大)"
我正在从我的PHP文件中共享代码。我也尝试在字段上创建索引,它们在集合之间创建连接但得到相同的结果。请告诉我创建集合结构和从MongoDB检索数据的最佳方法。
提前致谢。
$collection = new MongoCollection($db, 'col_main');
$condition = array();
$condition[] = array(
'$lookup' => array(
"from" => "col_a",
"localField" => "a",
"foreignField" => "_id",
"as" => "col_a"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_b",
"localField" => "b",
"foreignField" => "_id",
"as" => "col_b"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_c",
"localField" => "c",
"foreignField" => "_id",
"as" => "col_c"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_e",
"localField" => "e",
"foreignField" => "_id",
"as" => "col_e"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_f",
"localField" => "f",
"foreignField" => "_id",
"as" => "col_f"
)
);
$condition[] = array(
'$project' => $projection
);
$condition[] = array (
'$unwind' => '$col_a'
);
$condition[] = array (
'$unwind' => '$col_a.translation'
);
$condition[] = array (
'$unwind' => '$col_b'
);
$condition[] = array (
'$unwind' => '$col_b.translation'
);
$condition[] = array (
'$unwind' => '$col_c'
);
$condition[] = array (
'$unwind' => '$col_c.translation'
);
$condition[] = array (
'$unwind' => '$col_e'
);
$condition[] = array (
'$unwind' => '$col_e.staff_name'
);
$condition[] = array (
'$unwind' => '$col_e.staff_surname'
);
$condition[] = array (
'$unwind' => '$col_f'
);
$condition[] = array (
'$unwind' => '$col_f.translation'
);
//Some More conditions
$condition[] = array(
'$match' => array( 'col_b.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_a.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_c.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_f.translation.language' => $val )
);
return $collection->aggregate($condition);
答案 0 :(得分:0)
当您使用许多展开操作将对象相乘时,这可能会产生大量数据,每个$unwind
多个文档都会设置,因此可能会有大量内存消耗甚至交换。
如果我们每个有3个数组3个元素,那么我们将在a,b,c上执行展开 我们会得到: