title: mongoose link two collection with _id in express and insert id as number or string
body: <p>hello I want to create two collections first is room and have some attribute and _id as a primary key or object id I will insert it in post method and the other collection is booking and have some attribute and I want to store id or object id I insert it in the first collection in booking collections to link two collections with each other and can make populate</p>
  <pre><code> const mongoose=require('mongoose');
  const Schema =mongoose.Schema;
  const bcrypt=require('bcryptjs');
  const  schema  = mongoose.Schema;
  
   //---------- schema data -------------//
  const roomschema=Schema({
       _id:{
        type:String ,
        },
     floor:{
         type:String,
          required:true
      },
      status:{
          type:String,
          enum:['booking','busy','free'],
          default:'free'
      },
      price:{
          type:String,
      },
      bed:{
          type:Number
      },
      offer:{
       type:String,
       enum:['yes','no'],
       default:'no',
      } 
  });
  const room =mongoose.model('room',roomschema);
  
  const roombooking= Schema({
        _id:
        {
          type:Schema.Types.ObjectId,
         ref: 'room' ,
      },
        bed:{
          type:String,
       },
        price:{
            type:String,
            required:true,
        },
        from:{
        type:Date,
        default:Date.now,
        },
        to:{
          type:Date,
          default:Date.now,
          },
  });
  
  const booking =mongoose.model('booking',roombooking);
  module.exports={
      room:room,
      booking:booking
  };
  </code></pre>
  <p>this is the schema file I make it but I have a errors cast</p>
  <pre><code>Error: booking validation failed: _id: Cast to ObjectId failed for value &quot;12&quot; at path &quot;_id&quot;
      at ValidationError.inspect (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\error\validation.js:47:26)
      at formatValue (internal/util/inspect.js:725:31)
      at inspect (internal/util/inspect.js:293:10)
      at formatWithOptions (internal/util/inspect.js:1949:40)
      at Object.value (internal/console/constructor.js:301:14)
      at Object.log (internal/console/constructor.js:336:61)
      at C:\Users\DC\Desktop\datapage\hotelapptest\models\controller.js:103:17
      at processTicksAndRejections (internal/process/task_queues.js:97:5) {
    errors: {
      _id: CastError: Cast to ObjectId failed for value &quot;12&quot; at path &quot;_id&quot;
          at ObjectId.cast (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\schema\objectid.js:281:11)
          at ObjectId.SchemaType.applySetters (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\schematype.js:1091:12)
          at model.$set (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\document.js:1269:20)
          at model.$set (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\document.js:1013:16)
          at model.Document (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\document.js:148:12)
          at model.Model (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\model.js:105:12)
          at new model (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\model.js:4700:15)
          at booking (C:\Users\DC\Desktop\datapage\hotelapptest\models\controller.js:97:16)
          at Layer.handle [as handle_request] (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\layer.js:95:5)
          at next (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\route.js:137:13)
          at Route.dispatch (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\route.js:112:3)
          at Layer.handle [as handle_request] (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\layer.js:95:5)
          at C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\index.js:281:22
          at Function.process_params (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\index.js:335:12)
          at next (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\index.js:275:10)
          at Function.handle (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\index.js:174:3) {
        stringValue: '&quot;12&quot;',
        messageFormat: undefined,
        kind: 'ObjectId',
        value: '12',
        path: '_id',
        reason: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
            at new ObjectID (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\bson\lib\bson\objectid.js:59:11)
            at castObjectId (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\cast\objectid.js:25:12)
            at ObjectId.cast (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\schema\objectid.js:279:12)
            at ObjectId.SchemaType.applySetters (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\schematype.js:1091:12)
            at model.$set (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\document.js:1269:20)
            at model.$set (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\document.js:1013:16)
            at model.Document (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\document.js:148:12)
            at model.Model (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\model.js:105:12)
            at new model (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\mongoose\lib\model.js:4700:15)
            at booking (C:\Users\DC\Desktop\datapage\hotelapptest\models\controller.js:97:16)
            at Layer.handle [as handle_request] (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\layer.js:95:5)
            at next (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\route.js:137:13)
            at Route.dispatch (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\route.js:112:3)
            at Layer.handle [as handle_request] (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\layer.js:95:5)
            at C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\index.js:281:22
            at Function.process_params (C:\Users\DC\Desktop\datapage\hotelapptest\node_modules\express\lib\router\index.js:335:12)
      }
    },
    _message: 'booking validation failed'
  }
  </code></pre>
  <p>and this is insert in room collection method</p>
  <pre><code>insertroom = function(req,res){
  const {_id,floor,status,price,bed}=req.body;
  
  const newroom =new rooms.room({_id,floor,status,price,bed});
  newroom.save()
  .then(()=&gt;res.redirect('/getroom'))
  .catch((err)=&gt;console.log(err));
  };
  </code></pre>
  <p>and this is insert booking method</p>
  <pre><code>booking =function(req,res){
      const {_id,price,from,to,bed}=req.body;
      const message={}
      const fee =new rooms.booking({_id,price,from,bed,to});
      fee.save().then(()=&gt;{
          res.redirect('/updateroom',);
      })
      .catch((err)=&gt;{
          res.redirect('/dashboard');
          console.log(err)});
  
  }
  </code></pre>