$match not matches all the feilds inputed
00:20 06 Mar 2026

I am facing an issue with a MongoDB aggregation pipeline that uses the `$lookup` operator. The goal is to create a pipeline that filters data in my database based on input fields and returns the matching data using `$lookup`. However, when I request specific fields, I'm consistently receiving null values as the results. I suspect there might be a problem with my pipeline logic, and I'm seeking assistance in identifying and resolving it.

export const bookIssuedFind = async (req: Request, res: Response) => {
  try {
    const searchTerm = req.body || {};
    const filter:issued = {};

    if (searchTerm.issuer_id) filter.issuer_id = searchTerm.issuer_id;
    if (searchTerm.user_id) filter.user_id = searchTerm.user_id;
    if (searchTerm.issue_date) filter.issue_date = searchTerm.issue_date;
    if (searchTerm.return_date) filter.return_date = searchTerm.return_date;
    if (searchTerm.book_status) filter.book_status = searchTerm.book_status;
    if (searchTerm.book_id) filter.book_id = searchTerm.book_id;

    const pipeline = [];

    if (Object.keys(filter).length > 0) {
      pipeline.push({ $match: filter });
    }
    console.log(pipeline)
    pipeline.push(
      {
        $lookup: {
          from: "users",
          localField: "issuer_id",
          foreignField: "_id",
          as: "IssuerDetails"
        }
      },
      {
        $lookup: {
          from: "users",
          localField: "user_id",
          foreignField: "_id",
          as: "UserDetails"
        }
      },
      {
        $lookup: {
          from: "books",
          localField: "book_id",
          foreignField: "_id",
          as: "BookDetails"
        }
      }
      // { $unwind: { path: '$IssuerDetails', preserveNullAndEmptyArrays: true } },
      // { $unwind: { path: '$UserDetails', preserveNullAndEmptyArrays: true } },
      // { $unwind: { path: '$BookDetails', preserveNullAndEmptyArrays: true } },
    );
    console.log(pipeline)
    const issued = await Book_issueed.aggregate(pipeline).exec();
    console.log(issued)
    const result = (Object.keys(filter).length > 0) ? (issued[0] || null) : issued;
    return res.status(200).json(result);
  } catch (error) {
    if (error instanceof Error) {
      res.status(500).json({ message: `${error.message}` });
    }
  }
}


the output i am receiving is:


[ { '$match': { issuer_id: '69a6810495126821c9e08281' } } ]
[
  { '$match': { issuer_id: '69a6810495126821c9e08281' } },
  {
    '$lookup': {
      from: 'users',
      localField: 'issuer_id',
      foreignField: '_id',
      as: 'IssuerDetails'
    }
  },
  {
    '$lookup': {
      from: 'users',
      localField: 'user_id',
      foreignField: '_id',
      as: 'UserDetails'
    }
  },
  {
    '$lookup': {
      from: 'books',
      localField: 'book_id',
      foreignField: '_id',
      as: 'BookDetails'
    }
  }
]
[]
[ { '$match': { issuer_id: '69a6810495126821c9e08281' } } ]
[
  { '$match': { issuer_id: '69a6810495126821c9e08281' } },
  {
    '$lookup': {
      from: 'users',
      localField: 'issuer_id',
      foreignField: '_id',
      as: 'IssuerDetails'
    }
  },
  {
    '$lookup': {
      from: 'users',
      localField: 'user_id',
      foreignField: '_id',
      as: 'UserDetails'
    }
  },
  {
    '$lookup': {
      from: 'books',
      localField: 'book_id',
      foreignField: '_id',
      as: 'BookDetails'
    }
  }
]
[]
node.js mongoose