title: Push JSON Object into Mongoose Array
body: <p>I am fairly new to Mongoose. My goal is to be able to push data into a Mongoose array, so I can loop through it at a later time:
  Here is my schema:</p>
  
  <pre><code>var moduleSchema = new mongoose.Schema({
    name: {type: String, index: {unique: true}},
    priority: Number,
    author: String,
    enabled: Boolean,
    keywords: [{keyword: String}],
    code: String
  });
  </code></pre>
  
  <p>I would like to be able to send a JavaScript object in this format to the express and  have it inserted with its own "keyword" field. Ideally, the output should look like this:</p>
  
  <pre><code>{
      "_id": "56401885bff833582442bb5f",
      "name": "ModName",
      "priority": 1,
      "author": "tscrip",
      "enabled": true,
      "code": "TEST",
      "__v": 0,
      "keywords": [
          {
              "keyword": "Dog",
              "keyword": "Cat"
          }
       ]
  }
  </code></pre>
  
  <p>Every time I PUT data to update this, I either get:</p>
  
  <pre><code>0:D,
  1:O,
  2:G
  </code></pre>
  
  <p>Or I get:</p>
  
  <pre><code>The argument to $each in $push must be an array but it was of type Object
  </code></pre>
  
  <p>Here is my code to update the model:</p>
  
  <pre><code>if(req.body.keywords) updateObj.keywords = JSON.parse(req.body.keywords);
  
  var keywordsArr = updateObj.keywords;
  Module.findByIdAndUpdate({
      "_id": req.params.id
      },
      { $set: {keywords: { $each: { keyword: {keywordsArr}}}}},
      function(err, numAffected){
          console.log(err);
      }
  ); 
  </code></pre>
  
  <p>And I am sending it a simple JSON object to update:</p>
  
  <pre><code>{
      "keywords": [
          "mouse",
          "bird"
      ]
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I attempted to just make it an array:</p>
  
  <pre><code>var moduleSchema = new mongoose.Schema({
  name: {type: String, index: {unique: true}},
  priority: Number,
  author: String,
  enabled: Boolean,
  keywords: [String],
  code: String
  });
  </code></pre>
  
  <p>And update it like so:</p>
  
  <pre><code>if(req.body.keywords) updateObj.keywords = JSON.parse(req.body.keywords);
  var keywordsArr = updateObj.keywords.keywords;
  console.log(keywordsArr);
  Module.findByIdAndUpdate({
      "_id": req.params.id
      },
      { $push: {keywords: {$each: keywordsArr}}},
      function(err, numAffected){
          console.log(err);
      }
  );
  </code></pre>
  
  <p>But I now get an error:</p>
  
  <pre><code>{ [MongoError: exception: The field 'keywords' must be an array but is of type Object in document {_id: ObjectId('56401885bff833582442bb5f')}]
  name: 'MongoError',
  message: 'exception: The field \'keywords\' must be an array but is of type Object in document {_id: ObjectId(\'56401885bff833582442bb5f\')}'
  </code></pre>