title: Inserting multiple ObjectIds to sub-document at once with mongoose
body: <p>I'm using mongoose and I have the following model:</p>
  <pre><code>const UniversitySchema = new Schema({
      name: {
          type: String,
          required: [true, 'Name is required']
      },
      color: {
          type: String
      },
      paths: [{
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Path'
      }]
  })
  </code></pre>
  <p>I want to be able to insert multiple paths (which cast to the Path model) into the universities collection when first creating it. I tried the following code:</p>
  <pre><code>const newUni = new University({
      name: name,
      paths: [...pathIds],
      color: color
  })
  
  newUni.save()
      .then(uni =&gt; {
              return res.send(uni)
          })
      .catch(err =&gt; console.log(err)); 
      }) 
  </code></pre>
  <p>However it raises this error:</p>
  <pre><code>CastError: Cast to ObjectId failed for value &quot;[ 5f122f9967c59932b44214b6, 5f2762858f9060327c4f6b2f ]&quot; at path &quot;_id&quot; for model &quot;Path&quot;
  
   messageFormat: undefined,
    stringValue: '&quot;[ 5f122f9967c59932b44214b6, 5f2762858f9060327c4f6b2f ]&quot;',
    kind: 'ObjectId',
    value: [ 5f122f9967c59932b44214b6, 5f2762858f9060327c4f6b2f ],
    path: '_id',
    reason: Error: Argument passed in must be a single String of 12 bytes or a string 
  of 24 hex characters
  </code></pre>
  <p>It looks like mongoose does not cast the Ids separately when there are more than one, because when I tried it with one id, it worked.</p>
  <p>I also tried to save the documents and then push the ids to the path array, but it didn't work.</p>
  <p>Is there a way to do it all at once?</p>
  <p>Thanks in advance.</p>