title: How to define a schema that has nested arrays of objects
body: <p>Update: Wasn't using the collection object in the Schema for mongoose to specify what collection in the database to look at. Once I added the following to the mongoose.Schema it works fine.</p>
  <pre><code>{
      collection: &quot;maintenances&quot;
  }
  </code></pre>
  <p>New to mongoose schemas and having trouble with a more advanced object in MongoDB. Getting simple schemas to work seems fine but I have the following MongoDB schema that I am trying to replicate with a mongoose schema.</p>
  <pre><code>{
    &quot;_id&quot;: {
      &quot;$oid&quot;: &quot;61310bacd8195bf421751c16&quot;
    },
    &quot;jiraId&quot;: &quot;NA-151&quot;,
    &quot;summary&quot;: &quot;jira summary, trimmed to 50 chars&quot;,
    &quot;reason&quot;: &quot;maint reason&quot;,
    &quot;date&quot;: {
      &quot;$date&quot;: &quot;2021-07-01T05:00:00.000Z&quot;
    },
    &quot;createdDate&quot;: {
      &quot;$date&quot;: &quot;2021-08-01T05:00:00.000Z&quot;
    },
    &quot;pre&quot;: [
      {
        &quot;hostname&quot;: &quot;device1&quot;,
        &quot;displayName&quot;: &quot;device description&quot;,
        &quot;mopData&quot;: [
          {
            &quot;command&quot;: &quot;show clock&quot;,
            &quot;result&quot;: &quot;this is the show clock command pre&quot;
          },
          {
            &quot;command&quot;: &quot;show version&quot;,
            &quot;result&quot;: &quot;16.12.1&quot;
          }
        ]
      }
    ],
    &quot;post&quot;: [
      {
        &quot;hostname&quot;: &quot;device1&quot;,
        &quot;displayName&quot;: &quot;device description&quot;,
        &quot;mopData&quot;: [
          {
            &quot;command&quot;: &quot;show clock&quot;,
            &quot;result&quot;: &quot;this is the show clock command post&quot;
          },
          {
            &quot;command&quot;: &quot;show version&quot;,
            &quot;result&quot;: &quot;16.12.4&quot;
          }
        ]
      }
    ],
    &quot;deviceCount&quot;: 1
  }
  </code></pre>
  <p>I have tried several variations of js but its keeps returning empty arrays when doing a .find() on the collection.</p>
  <pre><code>import mongoose from &quot;mongoose&quot;;
  import shortid from &quot;shortid&quot;
  import {Main} from &quot;next/document&quot;;
  
  const {String, Number, Date} = mongoose.Schema.Types;
  
  const MaintenanceMopData = new mongoose.Schema({
      command: {
          type: String,
          required: true
      },
      result: {
          type: String,
          required: true
      }
  })
  
  const MaintenancePrePostSchema = new mongoose.Schema({
      hostname: {
          type: String,
          required: true
      },
      displayName: {
          type: String,
          required: true
      },
      mopData: [MaintenanceMopData],
  })
  
  const MaintenanceSchema = new mongoose.Schema({
      jiraId: {
          type: String,
          required: true
      },
      summary: {
          type: String,
          required: true
      },
      reason: {
          type: String,
          required: true
      },
      maintDate: {
          type: Date,
          required: false
      },
      createdDate: {
          type: Date,
          required: false
      },
      pre: [MaintenancePrePostSchema],
      post: [MaintenancePrePostSchema],
      deviceCount: {
          type: Number,
          required: false
      }
  })
  
  export default mongoose.models.Maintenance || mongoose.model('Maintenance', MaintenanceSchema)
  </code></pre>
  <p>Is this correct or what is the proper way to implement schema that has nested arrays of objects?</p>