Skip to content

The id field is missing when assigning a type to the Model. #15900

@xiejay97

Description

@xiejay97

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.0.1

Node.js version

22.18.0

MongoDB server version

8.2.2

Typescript version (if applicable)

5.9.3

Description

When I upgraded to the latest version, I found that the TypeScript checks failed because the document was missing the id field.

Steps to Reproduce

import { Schema, model, connect } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface IUser {
  name: string;
  email: string;
  avatar?: string;
}

// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: String
});

// 3. Create a Model.
const User = model<IUser>('User', userSchema);

run().catch(err => console.log(err));

async function run() {
  // 4. Connect to MongoDB
  await connect('mongodb://127.0.0.1:27017/test');

  const user = new User({
    name: 'Bill',
    email: '[email protected]',
    avatar: 'https://i.imgur.com/dM7Thhn.png'
  });
  await user.save();

  // Property 'id' does not exist on type 'Document<unknown, {}, IUser, {}, DefaultSchemaOptions> & IUser & { _id: ObjectId; } & { __v: number; }'.ts(2339)
  console.log(user.id);
}

Expected Behavior

No response

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