get id from object id mongodb code example

Example 1: where id and id in mongodb

const p_id = patient_id;
    let fetchingReports = await Reports.aggregate([
      ...(p_id
        ? [
            {
              $match: {
                createdBy: mongoose.Types.ObjectId(id),
                patient_id: p_id,
              },
            },
          ]
        : [
            {
              $match: {
                createdBy: mongoose.Types.ObjectId(id),
              },
            },

Example 2: mongo query by object id node js

//declare ObjectId from mongodb module
const ObjectId = require('mongodb').ObjectId; 

//transform your param into an ObjectId
var id = req.params.id;       
var good_id = new ObjectId(id);

//you can now query
Model.find({_id: good_id})