'Trying to find "magic combinations" in MongoDB
I have two MongoDB collections in a database named "records". Here's a sample document from the first collection:
course_completions
{
"_id" : NumberInt(12345),
"courses" : [
NumberInt(1),
NumberInt(2),
NumberInt(3),
NumberInt(4),
NumberInt(5),
NumberInt(11),
NumberInt(17),
NumberInt(22),
NumberInt(26),
NumberInt(74),
NumberInt(105),
NumberInt(118),
NumberInt(120),
NumberInt(139),
NumberInt(150),
NumberInt(253)
]
}
In the example above, the "_id" is the student number, and the "courses" are the course numbers.
Here is a sample document from the second collection:
cert_requirements
{
"_id" : {
"cert_name" : "ABC_Certification",
"value" : ObjectId("627aaf76793a17edfc9fd72e")
},
"certcourses" : [
NumberInt(74),
NumberInt(118),
NumberInt(120)
]
}
In this second collection, the "cert_name" (and there can be multiple documents with the same "cert_name" but different combinations of "certcourses") is the name of the certification they've earned IF the "certcourses" (array of "winning" combinations of course numbers) are all found in the first collection's "courses" array. The big challenge for me is that we do not have a single common field to match on using the $lookup function, but instead I'm trying to match based on the contents of arrays.
By the way, the above documents are actually the result of aggregation pipelines that distilled the information down this far. I'm just stumped on how to compare the arrays across collections.
Anyone have an idea how to solve this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|