title: Create new document with refs and supplying them with objectIds in mongodb using mongoose
body: <p>I am trying to use mongoose create function to create a new document that contains refs and i want to supply those objectsIds for those refs but i am getting an error and i cant seem to figure it out how to make it work. I tried to search for an answer but without any luck.</p>
  <p>I have the following model:</p>
  <pre><code>const mongoose = require('mongoose');
  const { Schema } = mongoose;
  
  const messageSchema = mongoose.Schema(
    {
      providerId: [{
        type: Schema.Types.ObjectId,
        ref: 'Provider'
      }],
      accountId: [{
        type: Schema.Types.ObjectId,
        ref: 'Account'
      }],
      conversationId: [{
        type: Schema.Types.ObjectId,
        ref: 'Conversation'
      }],
      type: {
        type: String,
        enum: ['provider', 'account'],
        default: 'provider',
      },
      status: {
        type: String,
        enum: ['queued', 'sent'],
        default: 'queued',
      },
      sendAt: {
        type: Date,
        default: Date.now()
      }
    },
    {
      timestamps: true,
    }
  );
  
  /**
   * @typedef Message
   */
  const Message = mongoose.model('Message', messageSchema);
  
  module.exports = Message;
  </code></pre>
  <p>And have the following code for the creation:</p>
  <pre><code>const prevMessage = await Message.findOneAndUpdate(
      { _id: match[1] },
      { $set: { status: placement.toLowerCase() } },
      {
          upsert: true,
          new: true
      }
  ).exec();
  
  const document = {
      providerId: { &quot;$oid&quot;: prevMessage.providerId[0] },
      accountId: { &quot;$oid&quot;: prevMessage.accountId[0] },
      conversationId: { &quot;$oid&quot;: prevMessage.conversationId[0] },
      type: &quot;provider&quot;,
      status: &quot;queued&quot;,
      sendAt: new Date(new Date().getTime() + 15 * 60000),
  }
  
  Message.create(document, { w: 1 }, function (err, res) {
      if (err) return logger.error(err);
  
      console.log(res);
  })
  </code></pre>
  <p>The prevMessage object looks as following:</p>
  <pre><code>{
    _id: new ObjectId(&quot;62590640993fcbb2027aa521&quot;),
    type: 'provider',
    status: 'sent',
    updatedAt: 2022-04-18T16:31:11.483Z,
    accountId: [ new ObjectId(&quot;623ccc5faa4d1a000cd1c608&quot;) ],
    conversationId: [ new ObjectId(&quot;625be027993fcbb202a6e682&quot;) ],
    createdAt: 2022-03-25T20:11:40.000Z,
    providerId: [ new ObjectId(&quot;625a97ff993fcbb202af92ee&quot;) ],
    sendAt: 2022-03-25T20:11:40.000Z
  }
  </code></pre>
  <p>The error that i get is the following:</p>
  <pre><code>error: ValidationError: Message validation failed: providerId.0: Cast to [ObjectId] failed for value &quot;`[ { '$oid': new ObjectId(&quot;625a97ff993fcbb202af92ee&quot;) } ]`&quot; (type string) at path &quot;providerId.0&quot;, accountId.0: 
  Cast to [ObjectId] failed for value &quot;`[ { '$oid': new ObjectId(&quot;623ccc5faa4d1a000cd1c608&quot;) } ]`&quot; (type string) at path &quot;accountId.0&quot;, conversationId.0: Cast to [ObjectId] failed for value &quot;`[ { '$oid': new ObjectId(&quot;625be027993fcbb202a6e682&quot;) } ]`&quot; (type string) at path &quot;conversationId.0&quot;
  </code></pre>
  <p>If anyone can help cause i cant figure it out how to create this document with refs that should an objectId value.</p>