As a successor to this topic: Group by on a string doesn't work, the aggregate query is very slow and not returning results anymore (no results is likely because of a timeout of 15 seconds that I added).
The query matches almost 2 million documents.
This is my document structure:
account_id (integer)
survey_id (integer)
answers (array)
0 (object)
answer
q
qid
1 (object)
answer
q
qid
etc.
This is the query:
$cursor = $collection->aggregate([
['$match' => ['account_id' => intval($values['account_id']), 'survey_id' =>
intval($values['survey_id'])]],
['$project' => ['answers.q' => 1, 'answers.qid' => 1]],
['$unwind' => '$answers'],
['$group' => ['_id' => '$answers.qid']]
]);
The goal of this query is to get all unique questions of the documents grouped by question id (qid).
Is there anyway this query can be faster? account_id, survey_id and qid have an index.