My MongoDB data structure looks like this:
{
users: [{
userref: "User1",
results: [{
testref: "TEST1",
history: [{
score: 10
}, {
score: 15
}]
}, {
testref: "TEST2",
history: [{
score: 2
}, {
score: 1
}]
}]
}]
}
I have a collection of users, each with an array of results (one result object for each test), and within each result, an array of the history of the historical scores.
What I need to do is "return the userref for all users whose most recent score for TEST1 is X and whose most recent score for TEST2 is Y". Users who have not completed one or other of these tests should not be returned. By "most recent", I mean the last in the array.
I'm assuming I need to use aggregation, but I'm totally new to this, so could do with some help.
This is what I have tried so far, and it works, but it only matches the condition where TEST1=15, and I would also like it to match the condition TEST2=1.
[{$unwind: {
path: '$results’,
preserveNullAndEmptyArrays: false
}}, {$match: {
'results.testref': 'TEST1'
}}, {$project: {
results: 1,
userref: 1,
}}, {$replaceRoot: {
newRoot: {
$mergeObjects: [
{
userref: '$userref'
},
'$results'
]
}
}}, {$project: {
userref: 1,
history: {
$slice: [
'$history',
-1
]
}
}}, {$match: {
'history.score': 15
}}, {$project: {
userref: 1,
}}]
Thanks
recentbased on your data?