Reputation: 123
I need to find the all viewed experiences from collections its working but there are duplicate entries i need to use distinct by expId i don't know how to use distinct here. please help i am new here.
function getViewedExperiences(){
return new Promise(function(resolve,reject){
Views.find({userId: req.userId},function(err,result){
if(err){
reject(err);
}else{
resolve(result);
}
});
});
}
my result is -
{
"msg": "Recently Viewed Experiences",
"data": [
{
"_id": "5aab6ad75ed3dc24ac5c492c",
"userId": "5aaa662357421667e989a286",
"expId": "5aab533972beff1c9880eb86",
"__v": 0,
"created": "2018-03-16T06:57:27.694Z"
},
{
"_id": "5aab6b475ed3dc24ac5c492d",
"userId": "5aaa662357421667e989a286",
"expId": "5aab52e272beff1c9880eb85",
"__v": 0,
"created": "2018-03-16T06:59:19.975Z"
},
{
"_id": "5aab6bc75ed3dc24ac5c492e",
"userId": "5aaa662357421667e989a286",
"expId": "5aab52e272beff1c9880eb85",
"__v": 0,
"created": "2018-03-16T07:01:27.973Z"
}
]
}
here how i use distinct??
Upvotes: 0
Views: 42
Reputation: 332
Try this:
function getViewedExperiences(){
return new Promise(function(resolve,reject){
Views.distinct('expId', {userId: req.userId},function(err,result){
if(err){
reject(err);
}else{
resolve();
}
});
});
}
Upvotes: 1