title: How can I save an empty array into mongodb using js
body: <p>Basically I got my app up an running but I'm stuck with a problem: if I pass an object that contains an empty array to be saved, the array is not saved into the db. I'm not sure this is a problem in js or the mongo driver, but in order to save the empty array I need to pass the array like so: <code>products: ['']</code>. </p>
  
  <p>This is the structure of my mongo document:</p>
  
  <pre><code>_id: ObjectId(...),
  name: 'String',
  subcategories: [
      {
          subcategory: 'string',
          products: [
              {
                   name: 'string'
                   price: integer
              }
          ]
      }
  ]
  </code></pre>
  
  <p>So in my front-end I'm grabbing the whole document through an ajax call pushing a new object into the subcategories array. The new object looks like this:</p>
  
  <p><code>{subcategory:'string', products:['']}</code></p>
  
  <p>And this works okay until I need to insert a new object inside the array: Because I've grabbed the whole object, pushed the new object to the array, the previous one looks like this:</p>
  
  <p><code>{subcategory: 'string'}</code></p>
  
  <p>Having lost the mention to <code>products:[]</code> array in the process.</p>
  
  <p>How can I get around this? I need to be able to have empty arrays in my object.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>What I did on front end: Got the whole object with <code>$.get</code> which returned:</p>
  
  <pre><code>var obj = 
  _id: ObjectId(...),
  name: 'String',
  subcategories: [
      {
          subcategory: 'Subcategory1',
          products: [
              {
                   name: 'string'
                   price: integer
              }
          ]
      }
  ];
  </code></pre>
  
  <p>Then on the front end I've pushed the new object category inside the subcategories array:</p>
  
  <pre><code>data.subcategories.push({subcategory: 'Subcategory2', products: ['']})
  </code></pre>
  
  <p>Where subcat was a string with the category name. On my db I could see that  I've successfully added the object:</p>
  
  <pre><code>var obj = 
  _id: ObjectId(...),
  name: 'String',
  subcategories: [
      {
          subcategory: 'Subcategory1',
          products: [
              {
                   name: 'string'
                   price: integer
              }
          ]
      },
      {
           subcategory: 'Subcategory2'
           products: []
      }
  ];
  </code></pre>
  
  <p>The problem was when I wanted to add another subcategory, the previous one return empty:</p>
  
  <pre><code>var obj = 
  _id: ObjectId(...),
  name: 'String',
  subcategories: [
      {
          subcategory: 'Subcategory1',
          products: [
              {
                   name: 'string'
                   price: integer
              }
          ]
      },
      {
           subcategory: 'Subcategory2'
      },
      {
           subcategory: 'Subcategory3'
           products: []
      },
  ];
  </code></pre>
  
  <p>Because at some point the empty array was removed from the object. Like I said, I did fix this in the front end, so the error jade was throwing has been addressed, but I still find odd that the <code>products: []</code> was being removed from the document.</p>
  
  <p>I'm new to MongoDb and node, not to mention that I'm also new with JS, so it might well be a feature that I'm unaware of.</p>