Hello everyone, I have the following structure:
{
id;
userId;
answersQuestion1: ["A1", "A2"],
answersQuestion2: ["A1", "A2", "A3"],
answersQuestion3: ["A1"]
}
The user will follow a wizard asking for several questions where only one answer can be selected. The documents may match more than one answer per question. Now I need to find all the documents where the given set of answer matches (exact or not) the answer specified in the documents. For example, lets say I have two questions
Q2: "Which programming language do you prefer?"
Answers: "Java", "C" "Python"
Q1: "What is your experience in this programming language?"
Answers: "None", "Low", "Medium", "Experienced"
and lets say I have a document as the following:
{
id;
userId;
answersQuestion1: ["C", "Python"],
answersQuestion2: ["None", "Low"]
}
And that the user will pick the following answers:
Q1: "C"
Q2: "Low"
At the moment I have an index for each answer_array-userId pair
ensureIndex("userId", "answersQuestion1"); ensureIndex("userId", "answersQuestion2"); ...
since we cannot have multikey indexes on arrays. And I just use an $in
operator to perform the query.
To me this does not look very efficient, I suppose mongo will pick the best index and perform a document scan to match the other answers.
I was wondering whether it would make sense to create a text containing something like:
C Python None Low
And perform a text search. I will then filter out the documents where the score less than a given threshold.
Potentially, for more granulated search I could use something like:
A1C A1Python A2None A2Low
And prefix the user answer with the A1 or A2.
This is just a guess, maybe Im completly off. Would that make sense?
Thank you