title: mongodb/mongoose - how to insert new subdocument with a nested array field
body: <p><strong>Original question</strong>: how to insert <code>type</code> as a field name.</p>
  
  <p>So I'm getting the error:</p>
  
  <pre><code>CastError: Cast to embedded failed for value "{ details:\n   [ { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b13741b\',\n       title: \'Name\',\n       order: 0 },\n     { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b13741a\',\n       title: \'Price\',\n       order: 1 },\n     { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b137419\',\n       title: \'Company\',\n       order: 2 } ] }" at path "items"
  </code></pre>
  
  <p>here's the object:</p>
  
  <pre><code>[
      {
          "title": "Name",
          "content": "",
          "hidden": false,
          "order": 0,
          "type": "text"
      },
      {
          "title": "Price",
          "content": "",
          "hidden": false,
          "order": 1,
          "type": "text"
      },
      {
          "title": "Company",
          "content": "",
          "hidden": false,
          "order": 2,
          "type": "text"
      }
  ]
  </code></pre>
  
  <p>I have the query:</p>
  
  <pre><code>ListModel
      .findOneAndUpdate(
          {
              _id: list_id
          },
          {   
              $push: {
                  items: {
                      details: the_template
                  }
              }
          }
      )
  </code></pre>
  
  <p>which is trying to make an insertion/update</p>
  
  <p>I'm not sure what the error is.</p>
  
  <p><strong>Noting</strong> that I am inserting a new <code>item</code> into the <code>items</code> array. It works if I create the item first, then update that item with a <code>$push: {details}</code> query.</p>
  
  <p>Here are the schemas.</p>
  
  <p>List Schema:</p>
  
  <pre><code>var List = new Schema(
      {
          items: {
              index: true,
              type: [Item]
          },
          hidden: {
              default: false,
              index: true,
              type: Boolean
          },
          name: {
              index: true,
              type: String,
          },
          item_details_template: {
              default: [
                  {
                      "title": "Name",
                      "content": "",
                      "hidden": false,
                      "order": 0,
                      "type": "text"
                  },
                  {
                      "title": "Price",
                      "content": "",
                      "hidden": false,
                      "order": 1,
                      "type": "text"
                  },
                  {
                      "title": "Company",
                      "content": "",
                      "hidden": false,
                      "order": 2,
                      "type": "text"
                  }
              ],
              type: [Item_Detail]
          },
          // Other users who have access
          shared_with: {
              index: true,
              refs: "users",
              type: [Shared_With]
          },
          // Owner
          user_id: {
              index: true,
              ref: "users",
              required: true,
              type: ObjectId,
          }
      },
      {
          strict: false
      }
  )
  </code></pre>
  
  <p>Item Schema:</p>
  
  <pre><code>var Item = new Schema(
      {
          // Template_Item
          based_on: {
              index: true,
              type: ObjectId
          },
          details: {
              default: [],
              index: true,
              type: [Item_Detail],
          },
          display_name: {
              default: "",
              index: true,
              type: String,
          },
          image: {
              default: "http://via.placeholder.com/700x500/ffffff/000000/?text=No%20Image&amp;",
              index: true,
              type: String
          },
          hidden: {
              default: true,
              index: true,
              type: Boolean
          },
          tags: {
              default: [],
              index: true,
              type: [Tag]
          }
      },
      {
          strict: false
      }
  )
  </code></pre>
  
  <p>ItemDetail Schema:</p>
  
  <pre><code>var Item_Detail = new Schema(
      {
          content: {
              default: "",
              index: true,
              type: String,
              trim: true,
              validate: {
                  validator(v)
                  {
                      if (this.title.trim() === "price")
                      {
                          return !isNaN(v.trim())
                      }
                      return true
                  }
              }
          },
          hidden: {
              default: false,
              index: true,
              type: Boolean
          },
          order: {
              index: true,
              required: true,
              type: Number
          },
          table: {
              default: {},
              type: Mixed
          },
          title: {
              required: true,
              index: true,
              type: String,
          },
          type: {
              default: "text",
              enum: ["text", "table"],
              index: true,
              type: String
          },
      },
      {
          strict: false
      }
  )
  </code></pre>