# mongoose-lean-virtuals

> Attach virtuals to the results of mongoose queries when using `.lean()`

Latest version **2.1.0** (published 2025-11-19) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install mongoose-lean-virtuals
pnpm add mongoose-lean-virtuals
yarn add mongoose-lean-virtuals
bun add mongoose-lean-virtuals
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-11-19 |
| First published | 2017-07-06 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=16.20.1 |
| Dependencies | 1 |
| Unpacked size | 34 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 49 |
| Author | Valeri Karpov |
| Maintainers | vkarpov15 |
| Keywords | mongoose, lean, virtuals, mongodb |

## Links

- npm: https://www.npmjs.com/package/mongoose-lean-virtuals
- Repository: https://github.com/mongoosejs/mongoose-lean-virtuals
- Issues: https://github.com/mongoosejs/mongoose-lean-virtuals/issues
- npm.io page: https://npm.io/package/mongoose-lean-virtuals

## Dependencies (1)

- [mpath](https://npm.io/package/mpath.md) ^0.8.4

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-11-19
- 2.0.1 — 2025-10-22
- 2.0.0 — 2025-05-14
- 1.1.1 — 2025-05-13
- 1.1.0 — 2025-01-05
- 1.0.0 — 2024-09-03
- 0.9.1 — 2022-04-28
- 0.9.0 — 2021-10-14
- 0.8.1 — 2021-09-18
- 0.8.0 — 2021-04-26
- 0.7.6 — 2020-12-15
- 0.7.5 — 2020-11-28
- 0.7.4 — 2020-11-20
- 0.7.3 — 2020-11-12
- 0.7.2 — 2020-10-13
- … 27 more at https://npm.io/package/mongoose-lean-virtuals/versions

## README

# mongoose-lean-virtuals

Attach virtuals to the results of mongoose queries when using [`.lean()`](https://mongoosejs.com/docs/api.html#query_Query-lean).

[Read the docs here](http://plugins.mongoosejs.io/plugins/lean-virtuals).

## Security contact information

To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.

## Usage

```javascript
const mongooseLeanVirtuals = require('mongoose-lean-virtuals');

// Example schema
const userSchema = new mongoose.Schema({ name: String });

userSchema.virtual('lowercase').get(function() {
  return this.name.toLowerCase();
});

// Now, the `lowercase` property will show up even if you do a lean query
userSchema.plugin(mongooseLeanVirtuals);

// Later

// You **must** pass `virtuals: true` to `lean()`, otherwise `lowercase`
// won't be in `res`
const res = await UserModel.find().lean({ virtuals: true });
```

## TypeScript

Mongoose's `lean()` function typings don't know about `virtuals: true`, so you need to explicitly set the type when calling `lean()`.
This module exports a convenient `VirtualsForModel` helper type that returns the virtual property types for a given model.
The below example shows using `VirtualsForModel` along with `lean<TypeOverride>()`.

```ts
import mongooseLeanVirtuals, { VirtualsForModel } from "mongoose-lean-virtuals";

interface ITest {
  name: string
}

const testSchema = new mongoose.Schema(
  { name: { type: String, required: true } },
  {
    virtuals: {
      nameUpper: {
        get() {
          return this.name.toUpperCase();
        }
      }
    }
  }
);

testSchema.plugin(mongooseLeanVirtuals);

const TestModel = mongoose.model('Test', testSchema);

TestModel.findOne().lean<ITest & VirtualsForModel<typeof TestModel>>({ virtuals: true }).orFail().then(doc => {
  const name: string = doc.name;
  const nameUpper: string = doc.nameUpper;
});
```

---
_Source: https://npm.io/package/mongoose-lean-virtuals · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
