Friday 2 June 2017

$nin is not working on aggregate ? Any substitute?

I am trying to implement the functionality of $nin in aggregate and I understand maybe it only works on $find. I am working on swipe card functionality, where a certain number of users are liked, which I collect and then show users only which fall in the category but are not liked or disliked.Below is the code, how do get the same functionality of $nin ?

let checkedFreelancers = [];

let checkedFreelancersQuery = {
    missionId: new ObjectId(missionId)
}

connection.collection("checkedFreelancerMissionBoard").find(checkedFreelancersQuery).toArray(
        (err,freelancerresult)=>{
        if(err){
            console.log(err);
        }else{
            for(var i=0;i<freelancerresult.length;i++){
                checkedFreelancers.push(freelancerresult[i].userId);
            }
        }

        });

let matchQuery = {
    "missionId" : new ObjectId(missionId),
    "isLiked" : true,
    "userId" : {
        $nin : checkedFreelancers
    }
}
let sortKey = {created: -1}

let aggregation = [
        {$match: matchQuery},
        {$lookup:
            {
                from: 'users',
                localField: 'userId',
                foreignField: '_id',
                as: 'users_data'
            }
        },
        {$project:
            {
                _id: 1,
                missionId: 1,
                userId: 1,
                created: 1,
                users_data:
                    {
                        $arrayElemAt: ['$users_data.content', 0]
                    },
                locationData: { $arrayElemAt: ['$users_data.locationData', 0]}
            }
        },
        {$sort: sortKey},
        {$skip:offset},
        {$limit: count}

    ];
    utility.validateSession(connection, sessionKey, (err, user) => {
        if (err) {
            console.log(err);
            return utility.errorResponse(res, err);
        }else{
            connection.collection('checkedMissionBoard').aggregate(aggregation).toArray(
            (err, result) => {
            if (err) {
                console.log(err);
                return utility.errorResponse(res, 500);
            }
                return utility.successResponse(res, result);
            });
        }
    });

};



via Hemant Kumar Goyal

No comments:

Post a Comment