Unable to use explain() with aggregation pipeline

  1. In below code when ever I am trying to use explain() getting this error,“MongoInvalidArgumentError: Option “explain” cannot be used on an aggregate call with writeConcern”.But why where I am doing wrong?

  2. And another thing is this aggregation pipeline takes 30 sec to return 30000 data,which is very slow how can i optimize this?

Note:I have alreday created Indexes in corresponding collection but still slow.
Please help me to achieve this, Thanks in advance…

const result1 = await this.qcInspectionModel.aggregate([
  {
    $match: {
      $and: [
        startDateQuery,
        endDateQuery,
        statusQuery,
        businessUnitFilterQuery,
        { isDeleted: false },
        { isSLCMQcInspection: true },
      ],
    },
  },
  {
    $lookup: {
      from: 'mastercommodities',
      localField: 'commodityId',
      pipeline: [
        {
          $project: {
            name: 1,
          },
        },
      ],
      foreignField: '_id',
      as: 'commodityData',
    },
  },
  {
    $unwind: '$commodityData',
  },
  {
    $lookup: {
      from: 'commodityvariants',
      localField: 'commodityVariantId',
      pipeline: [
        {
          $project: {
            name: 1,
          },
        },
      ],
      foreignField: '_id',
      as: 'commodityVariantData',
    },
  },
  {
    $unwind: '$commodityVariantData',
  },
  {
    $lookup: {
      from: 'businessunits',
      localField: 'businessUnitId',
      pipeline: [
        {
          $lookup: {
            from: 'businesses',
            localField: 'businessId',
            foreignField: '_id',
            as: 'businessClientName',
          },
        },
        {
          $unwind: '$businessClientName',
        },
        {
          $project: {
            name: 1,
            businessClientName: '$businessClientName.displayName',
          },
        },
      ],
      foreignField: '_id',
      as: 'businessUnitData',
    },
  },
  {
    $unwind: {
      path: '$businessUnitData',
      preserveNullAndEmptyArrays: true,
    },
  },
  {
    $lookup: {
      from: 'users',
      localField: 'createdBy',
      foreignField: '_id',
      as: 'userData',
      pipeline: [
        {
          $project: {
            firstName: 1,
            lastName: 1,
            _id: 0,
            name: { $concat: ['$firstName', ' ', '$lastName'] },
          },
        },
      ],
    },
  },
  {
    $unwind: {
      path: '$userData',
      preserveNullAndEmptyArrays: true,
    },
  },
  {
    $match: {
      $and: [
        commoditySearchQuery,
        variantSearchQuery,
        generalSearchQuery,
        cidNumberSearchQuery,
        lotNoSearchQuery
      ],
    },
  },
  {
    $sort: {
      createdAt:
        filters.sortOrder && filters.sortOrder != SortOrder.Ascending
          ? SortOrder.Descending
          : SortOrder.Ascending,
    },
  },
  {
    $project: {
      _id: 1,
      status: 1,
      commodityData: 1,
      commodityDetail: 1,
      commodityVariantData: 1,
      createdAt: 1,
      qcId: 1,
      sampleName: 1,
      businessUnitData: 1,
      userData: 1,
      location: 1,
      middlewareStatus: 1,
    },
  },
  {
    $facet: {
      records: [
        { $skip: (filters.pageNumber - 1) * filters.count },
        { $limit: filters.count * 1 },
      ],
      total: [{ $count: 'count' }],
    },
  }
]).explain();