I have a mongoose model 'OnlineUsers' which has schema
{user_id : ObjectId,
params : [Number] // Usually [1,25,4,32,3] , length ~ 5
top5 : [Number] // [1,4,25,32,7]
}
I have a array reqParams = [1,23,31,3,5]
I need to find the most compatible OnlineUsers based on score 0.3.S1 + 0.4.S2 + 0.3.S3
where S1,S2 and S3 are obtained through different aggregation queries.
The problem : I need a single aggregation query to perform all the three steps and give me _id and score sorted {S : -1}
For S1 :
OnlineUsers.aggregate([
{'$match' : {$ne : {_id : userId}}}, //
{'$unwind' : '$params'},
{'$match' : {$in : reqParams }},
{'$group' : {{'_id' : '$_id'},{S1 : {$sum : 1 }}},
{'$project : {S1Score : {$divide : ['$S1',5]}}} // Is 5 correct or do i need a field in the document ?
],cb)
Similarly i write the same for S2 , replacing
$unwind : '$top5'
$match : { $in : userTop5 },
and for S3
$unwind : '$params',
$match : {$in : userParams }
where userTop5 and userParams are both of the form [1,23,35,21,5]
I need the result in a single aggregation query which returns _id and score S. This is a realtime query and the response is critical , initially there might be 200-500 people , ie docs in collection . but it maybe thousands .
Can i use map/ reduce ? The response is realtime
Sorry i had to copy paste from StackOverflow