找到不同的数据

时间:2018-03-16 07:46:53

标签: node.js mongodb mongoose

我需要找到其收藏的所有观看体验,但是我需要使用不同 expId 我不知道怎么做在这里使用distinct。请帮助我新来的。

function getViewedExperiences(){
    return new Promise(function(resolve,reject){
        Views.find({userId: req.userId},function(err,result){
            if(err){
                reject(err);
            }else{
                resolve(result);
            }
        });
    });
}

我的结果是 -

{
"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"
    }
  ]
}

这里我如何使用不同?

1 个答案:

答案 0 :(得分:0)

试试这个:

function getViewedExperiences(){
    return new Promise(function(resolve,reject){
        Views.distinct('expId', {userId: req.userId},function(err,result){
            if(err){
                reject(err);
            }else{
                resolve();
            }
        });
    });
}