Friday 2 June 2017

How to get the unique results from mongoose using $setUnion and $group?

sample documents of mongodb

I tried something like below but dint get the expected result , This should work if the fields are not an array

 // union of includes and excludes as excludesAndExcludes
    getIncludesAndExcludes: (req, res)=>{
        console.log('called setunion');
        experienceModel.aggregate([
            { $group: {_id : {includes:"$includes", excludes:"$excludes"}}},            
            { $project: { includesAndExcludes: { $setUnion: [ "$_id.includes", "$_id.excludes" ] }, _id:0 } }  
        ], (err, data) => {
            if (err) {
                res.status(500).send(error); 
            } else {
                res.json(data);
            }         
        })
    },



via kartikajas

No comments:

Post a Comment