排除匹配MongoDB中的用户

时间:2017-01-31 23:02:46

标签: mongodb mongodb-query

我想在查询中添加Not。但不幸的是我没有得到所需的结果。我想获得用户不等于userid的结果。但我对如何添加它感到困惑。我试过多个场景但都失败了。

server.get('/myFeedback', (req, res) => {
            var userid = req.query.userID;
            //console.log(req.query);
        db.collection("tweetsWithSentimentFeedback").aggregate( [
                              {
                                $group: {
                                   _id: {
                                        topic: "$topic",
                                        group : "$group",
                                        type : "$type", 
                                        user    :   "$userName"
                                   },
                                   count: { $sum: 1 }    
                                  }
                              },{    $group: {
                                    _id: {
                                        topic: "$_id.topic",
                                        group : "$_id.group",

                                        },
                                    typeAndCount: {
                                        $addToSet: {
                                            type: "$_id.type",
                                            count: "$count"
                                        }
                                    },
                                    userName: {
                                        $addToSet: {
                                            user: "$_id.userName"
                                        }
                                    },
                                    totalCount: {
                                        $sum: "$count"
                                    }
                                }
                      },
                      { $match: { $and: [ { totalCount: { $gt: 0, $lt: 15 } }, {userEqual: { $ne: [ "$userName.user", userid ] }} ] } },
                            // Then sort
                               { "$sort": { "totalCount": -1 } }
                           ], (err, result) => {
                               if (err) {
                                   console.log(err);
                               }
                               res.status(200).send(result);
                           } );
    });

1 个答案:

答案 0 :(得分:2)

您应该添加$match作为第一阶段来过滤user

{ $match: { userName: { $ne: userid } } }

更新

db.collection("tweetsWithSentimentFeedback").aggregate(
    [{
        $group: {
            _id: {
                topic: "$topic",
                group: "$group",
                type: "$type",
                user: "$userName"
            },
            count: {
                $sum: 1
            }
        }
    }, {
        $group: {
            _id: {
                topic: "$_id.topic",
                group: "$_id.group"
            },
            typeAndCount: {
                $addToSet: {
                    type: "$_id.type",
                    count: "$count"
                }
            },
            userName: {
                $addToSet: "$_id.userName"
            },
            totalCount: {
                $sum: "$count"
            }
        }
    }, {
        $match: {
            {
                totalCount: {
                    $gt: 0,
                    $lt: 15
                }
            },
            {
                userName: {
                    $ne: userid
                }
            }
        }
    }, , {
        $sort: {
            totalCount: -1
        }
    }])