Story Maker
    Preparing search index...

    Function serializeMongoDocument

    • Serializes a MongoDB document or any nested structure containing MongoDB-specific types. This function handles ObjectId and Date objects, converting them to strings and ISO strings respectively. It also recursively processes nested objects and arrays.

      Parameters

      • doc: unknown

        The document or value to serialize. Can be of any type.

      Returns unknown

      The serialized version of the input. ObjectIds are converted to strings, Dates to ISO strings, and nested objects/arrays are recursively processed.

      const mongoDoc = { _id: new ObjectId("507f1f77bcf86cd799439011"), date: new Date() };
      const serialized = serializeMongoDocument(mongoDoc);
      // Result: { _id: "507f1f77bcf86cd799439011", date: "2023-04-01T12:00:00.000Z" }