I have a collection like this:
[
{"userId": "0000", "algorithm": "algo1", "status": "Running", "waitingTime": 0},
{"userId": "0001", "algorithm": "algo1", "status": "Received", "waitingTime": 0},
{"userId": "0000", "algorithm": "algo2", "status": "Completed", "waitingTime": 123},
{"userId": "0000", "algorithm": "algo2", "status": "Error", "waitingTime": 134},
{"userId": "0001", "algorithm": "algo2", "status": "Error", "waitingTime": 150},
{"userId": "0001", "algorithm": "algo3", "status": "Completed", "waitingTime": 100},
{"userId": "0000", "algorithm": "algo3", "status": "Completed", "waitingTime": 120},
{"userId": "0001", "algorithm": "algo1", "status": "Received", "waitingTime": 0}
]
I need to find each user's maximum waiting time but only with status "Completed". The output should have 2 props for each document.
- "_id" - which should be "userId" from original collection
- "maxWaitingTime" - should be equal to the maximum waiting time for status "Completed"
For this example the output should be something like this:
[
{"_id": "0000", "maxWaitingTime": 123},
{"_id": "0001", "maxWaitingTime": 100}
]
I tried to combine the some filtering queries together but it didn't worked out well. I can combine one or two statements but for this one I'm strugle.
I tried to combine the distinct method with find to get unique userId's but it didn't worked:
db.tasks.distinct("userId").find();
Also I tried to do the logic inside of the find fucntion:
printjsononeline(db.tasks.distinct(userId).find({
userId: "0001"
}).map(doc => doc));
But unfortunetly it doesn't worked eather.