I have three Collections:
UserCollection, DepartmentCollection and QuestionsCollection
QuestionsCollection is a configurable list of questions.
Users and Departments each answer a survey generated from QuestionsCollection and as a result each have an embedded array of questionAnswer objects with each object having a questionid and an answer property.
An example User document from the UserCollection would be:
{
id:SoEtnNvN8B3QYcd3N,
questionAnswers:[
{
questionId: 1,
answer: "melbourne"
},
{
questionId: 2,
answer: "10"}]
}
}
A department would have the same embedded array but could contain more or less questionAnswer objects. With different or same answers of course.
The problem:
What i'd like to do is be able to get a single Users questionAnswers array as the search criteria, and see how many departments match those questionAnswers.
The matching would match on both properties, and a user must answer the same questions a department has answered in order to match e.g.
- If the department has answered more questions than the user it's automatically not a match.
- If the user and department have answered the same number of questions then all questionAnswer values should match.
- If a user has answered more questions than a department, then all the departments questionAnswers must be contained within the users questionAnswers.
The answers would be exact matches, though I might need a mechanism for ranged searches at some point.
I've looked into maybe building a query using the users questionAnswers array and using $elemMatch to search on the DepartmentCollection, but I'm not sure how to implement this. Any help or direction would be appreciated.