title: How to get _id of Models in Mongoose (Typescript)
body: <p>I'm using Mongoose to model mongodb object (node.js / nestJS)</p>
  <h3>Goal</h3>
  <p>I want to have the <code>_id</code> - which is automatically created by MongoDB - of an object in a POJO exposed. Because I need them on client side for some operations. (identify same objects, ...)</p>
  <h3>What I tried</h3>
  <pre><code>export type GroupDocument = Group &amp; Document;
  
  @Schema()
  export class Group {
      @Prop({ required: true })
      name: string;
  }
  </code></pre>
  <p>Given the code above, I can access the <code>_id</code> when I use</p>
  <pre><code>const group: GroupDocument = this.findOne();
  </code></pre>
  <p>which is fine. In most of the cases I don't want to work with the whole <code>GroupDocument</code> object, because it has a lot of overhead and it's recommended to work with <code>LeanDocuments</code> as much as possible. But when I call <code>.lean()</code> on a GroupDocument, the <code>_id</code> gets lost and I can't access it anymore.</p>
  <pre><code>const group: = this.findOne().lean();
  </code></pre>
  <p>It works if I would extend my Group from Documents like so:</p>
  <pre><code>Group extends Document
  </code></pre>
  <p>But this isn't recommended by mongoose</p>
  <p>I also thought about just putting the _id into the POJO like so:</p>
  <pre><code>@Schema()
  export class Group {
  
      _id: string;
  
      @Prop({ required: true })
      name: string;
  }
  </code></pre>
  <p>But this seems to me not an ideal solution.</p>
  <p>All I want to do is to make it possible to call <code>group.id</code> / <code>group._id</code> on a lean object. Is there any way to achieve that without harming any best practices?</p>