FG
๐Ÿ’ป Software๐Ÿ—„๏ธ DatabasesMongoDB

LeanDocument Type doesn't include _id or id

Fresh3 days ago
Mar 14, 20260 views
Confidence Score55%
55%

Problem

*Do you want to request a feature or report a bug? Report a bug What is the current behavior? I believe that the types defined here are incorrect: https://github.com/Automattic/mongoose/blob/c22152c5a95c5ce9ed688d96b37b05c7df6be1ec/types/index.d.ts#L2261-L2267 This says that `LeanDocuments` (that is documents returned from `.toObject()` or when using the `lean` option don't have the `_id` or `id` fields. I don't think this is correct. This issue only presented itself in version 6.3.2 and wasn't present in 6.3.1 If the current behavior is a bug, please provide the steps to reproduce. [code block] What is the expected behavior? `_id` and `id` are present on LeanDocuments (I think...) What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.* Node.js: v16.15.0 TypeScript: v4.6.2 mongoose: v6.3.2

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix LeanDocument Type to Include _id and id Fields

Medium Risk

In version 6.3.2 of Mongoose, the TypeScript definitions for LeanDocuments incorrectly omit the _id and id fields. This change was not present in version 6.3.1, leading to confusion and potential errors in TypeScript applications that expect these fields to be included.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Identify the TypeScript Definitions File

    Locate the TypeScript definitions file for Mongoose where LeanDocument types are defined. This can be found in the Mongoose GitHub repository under types/index.d.ts.

    yaml
    https://github.com/Automattic/mongoose/blob/c22152c5a95c5ce9ed688d96b37b05c7df6be1ec/types/index.d.ts
  2. 2

    Modify LeanDocument Type Definitions

    Edit the LeanDocument type definition to include the _id and id fields. This can be done by creating a custom type definition file in your project that extends the existing Mongoose types.

    typescript
    declare module 'mongoose' {
      interface LeanDocument<T> {
        _id: string;
        id: string;
      }
    }
  3. 3

    Test the Changes

    Run your TypeScript application and ensure that LeanDocuments now correctly include the _id and id fields. You can create a simple test case that retrieves a document using the lean option and checks for the presence of these fields.

    typescript
    const doc = await Model.findOne().lean();
    console.log(doc._id, doc.id); // Should log the _id and id values
  4. 4

    Update TypeScript Configuration

    Ensure that your TypeScript configuration is set to recognize the custom type definitions. This might involve adding the path to your custom type definitions in the tsconfig.json file.

    json
    {
      "compilerOptions": {
        "typeRoots": ["./node_modules/@types", "./path/to/custom/types"]
      }
    }
  5. 5

    Document the Changes

    Update your project documentation to reflect the changes made to the LeanDocument type definitions. This will help other developers understand the modifications and ensure consistency across the codebase.

    markdown
    Update README.md with information about LeanDocument type changes.

Validation

Confirm that LeanDocuments returned from Mongoose queries now include the _id and id fields by running the test case and checking the output. Additionally, ensure that TypeScript does not throw any errors related to these fields.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodmtypescript