Wednesday, 26 April 2017

MongooseJs: How to remove a null or empty object from sub-document array

I have a very simple exam solution MongooseJs Document.

{ 
   exam_name: String,
   solutions: [
       question: String,
       body: String,
       answer: String
   ]
}

My problem is that when i inset an object into my solutions sub-document array there is some null or empty object inserted into the array. How can i remove this null or empty object from my solution Array using MongooseJs find or query method.

JSON Output:

{
  "_id": "59006497a105b814307a33f1",
  "exam_name": "english",
  "__v": 1,
  "solutions": [
    {
      "question": "first question?",
      "answer": " first question answer",
      "_id": "59006497a105b814307a33f3"
    },

    null,

   {
      "question": "second question?",
      "answer": "second question answer",
      "_id": "590067544867a3226c0cf0d1"
    }
  ]
}

You can see a null object also in my output. Please give me a appropriate solution for this problem.



via Rahadur

No comments:

Post a Comment