Skip to content

InferRawDocTypeFromSchema type missing _id #16053

@lobabob

Description

@lobabob

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

9.2.3

Node.js version

22.14

MongoDB server version

8.2.3

Typescript version (if applicable)

5.7.3

Description

I expect the InferRawDocTypeFromSchema type to be the same as what's returned from .lean(). As far as I understand it, it should result in the same output as InferRawDocType which does have _id.

Steps to Reproduce

const schemaDefinition = new Schema({
  email : { 
    type : String ,
    trim: true,
    required : true,
    unique: true,
    lowercase: true
  },
  password : {
    type : String ,
    required : true,
  },
  dateOfBirth: {
    type: Date,
    required: true
  }
})

const TestModel = mongoose.model('Test', schemaDefinition);
type TestType = mongoose.InferRawDocTypeFromSchema<typeof schemaDefinition>;

let test: TestType | null;

async function queryById(id: string ){
    let test: TestType | null ;
    TestModel.findById(id).lean().then(result=>{
        if (result){
            test = result
            console.log(result._id)
            console.log(test._id) // This throws a type error since _id doesn't exist on the type when it should
        }
    })
}

Expected Behavior

I would expect the _id to exist here since it's technically part of the schema. I would expect it to behave the same as InferRawDocType.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions