node.jsmongodbrestify

In Mongo-DB How to set Default Value for a field through node restify?


I have created a collection(table) named Group and I have fields such as Group name,Group Desc and Group Type but I need to set the Group type by default to 1 if it receives a null value. And I have the following code to insert.

function postNewGroups(req , res , next){

var Group = {};
Group._id = sequence;
Group.GROUP_NAME = req.params.GROUP_NAME;
Group.GROUP_DESC = req.params.GROUP_DESC;
Group.GROUP_TYPE = req.params.GROUP_TYPE;

Groups.save(Group , function(err , success){
        console.log('Response success '+success);
        console.log('Response error '+err);
        if(success){
            res.send(201 , Group);
            return next();
        }else{
            return next(err);
        }
    });

But I have searched lot about how to set a default value but I couldn't able to find a solution.


Solution

  • If you like to set default value while inserting data in MongoDb, Use mongoose. It has functionality multiple functionality given below and many more.
    for Ex.

    {
    organization:{
      type: string,
      default: "XYZ"
     }
    }