How can I create new fields in return results and set values in them on the basis of conditions using Mongoose? How can I create new fields in return results and set values in them on the basis of conditions using Mongoose? mongoose mongoose

How can I create new fields in return results and set values in them on the basis of conditions using Mongoose?


I finally firgured it out. Use switch statements

AnimalsModel.aggregate([    {        $project: {            "_id": 1,            "status": 1,            "name": 1,            "new_field": {                "$switch": {                    "branches": [                        { "case": { "$eq": [ "$status", "wild" ] }, "then": 'Tough Forest Animal' },                        { "case": { "$eq": [ "$status", "domestic" ] }, "then": 'Friendly Neighborhood Animal' },                    ],                    "default": 'None of the above'                }            }        }    }],function (error, animals) {    console.log(animals)});