Monday 13 March 2017

Ho do I retrieve parent document by its id without sub document in nodejs application?

I have a schema like parent document is User and sub-document is batchSchema. Here my schema is

var batchSchema = new Schema({
   batchname: {
        type: String,
        required: true,
        unique: true
    },
    activityname: {
        type: String,
        required: true
    },
    time: {
    type:String,
    required:true
    },
    duration: {
    type:String,
    required:true
    },
    classtype: {
    type:String,
    required:true
    },
    trainer: {
    type:String,
    required:true
    },
    price:{
    type:Currency,
    required:true,
    unique:true
    },

}, {
    timestamps: true
});

var User = new Schema({
username:String,
firstname:String,
lastname:String,
password:String,
batches:[batchSchema]

});

when I want to see the details of user with the help of id without batchSchema details then it prints both.And the code for that is

router.get('/:userId',function (req, res, next) {
    User.findById(req.params.userId, function (err, user) {
        if (err) throw err;
        res.json(user);
    });
});

and you can see the example of my problem what I am asking.

{
  "_id": "58c66ed333a2171acc603481",
  "username": "joe",
  "firstname": "jain",
  "lastname": "jam",
  "__v": 1,
  "batches": [
    {
      "updatedAt": "2017-03-13T10:06:48.341Z",
      "createdAt": "2017-03-13T10:06:48.341Z",
      "batchname": "rock",
      "activityname": "fit",
      "time": "1:30pm",
      "duration": "175min",
      "classtype": "demo",
      "trainer": "boby",
      "price": "4568",
      "_id": "58c66f3833a2171acc603482"
    }
  ]
}

So, I want to know how to print only parent document without sub document?



via Syed Ayesha Bebe

No comments:

Post a Comment