Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mongoose-lean-getters

mongoosejs125.8kApache 2.02.2.2TypeScript support: included

Apply getters to the results of mongoose queries when using .lean()

mongoose, lean, getters, mongodb

readme

mongoose-lean-getters

Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html

This package requires Mongoose >= 7.1.0. Do not use with Mongoose 7.0.x or 6.x.

Usage

const mongoose = require('mongoose');
const mongooseLeanGetters = require('mongoose-lean-getters');

const schema = mongoose.Schema({
  name: {
    type: String,
    // Get the last 6 characters of the string
    get: v => v.slice(-6)
  }
});
// Add this plugin to apply getters when using `lean()`.
schema.plugin(mongooseLeanGetters);

await Model.create({ name: 'Captain Jean-Luc Picard' });

const doc = await Model.findOne().lean({ getters: true });
doc.name; // 'Picard'

You may also set the default lean options to always use getters:

const mongoose = require('mongoose');
const mongooseLeanGetters = require('mongoose-lean-getters');

const schema = mongoose.Schema({
  name: {
    type: String,
    // Get the last 6 characters of the string
    get: v => v.slice(-6)
  }
});
// Set the default options for all lean queries
schema.plugin(mongooseLeanGetters, { defaultLeanOptions: { getters: true }});

await Model.create({ name: 'Captain Jean-Luc Picard' });

const doc = await Model.findOne().lean();
doc.name; // 'Picard'

// You may also set getters: false at call time
const doc2 = await Model.findOne().lean({ getters: false });
doc2.name; // 'Captain Jean-Luc Picard'

changelog

2.2.2 / 2025-05-12

  • fix: don't apply getters if defaultLeanOptions is set but lean is false #48 #46
  • fix: apply single nested subdoc getter results #47 #45

2.2.1 / 2025-02-24

  • fix: handle getter for single nested that returns non-object #42

2.2.0 / 2025-02-21

  • feat: allow default lean options plugin config #41 DesignByOnyx
  • fix: allow nested schemas on discriminated models to work #43 DesignByOnyx
  • fix: allow array getters to return non-arrays fix #38 #37 DesignByOnyx

2.1.1 / 2024-08-04

  • fix: allow non-discriminated documents be retrieved #40 DesignByOnyx
  • fix: don't throw error if array contains null #36 Sebmaster

2.1.0 / 2024-04-27

  • feat: add support for findOneAndReplace #34 Ebulus7899
  • fix: handle discriminators in nested document arrays #32 nathan-knight

2.0.1 / 2024-03-08

  • fix: handle discriminators with explicit tied values #31 MarkParnwell
  • fix: correctly get schema for each element of a discriminated array #28 nathan-knight

2.0.0 / 2024-03-07

  • BREAKING CHANGE: call getters correctly on array elements for Mongoose 7.5.0, require Mongoose 7.5.0 #30

1.1.0 / 2023-06-01

  • feat: apply getters to schemas with discriminator #26 remcorakers

1.0.0 / 2023-04-27

  • BREAKING CHANGE: require Mongoose >= 7.1
  • fix: avoid calling getters on excluded paths in arrays #22 IslandRhythms

0.4.0 / 2023-02-13

0.3.6 / 2023-01-06

0.3.5 / 2022-07-22

0.3.4 / 2022-06-19

  • fix: call nested getters if nesting level > 1 #17 vladomnifi

0.3.3 / 2022-05-28

  • fix(types): remove unnecessary empty index.ts file #14 #15

0.3.2 / 2022-04-20

  • fix: correctly handle projections with document arrays #13
  • fix: export default for ESM imports #11 IslandRhythms

0.3.1 / 2022-04-17

  • fix: avoid running getters on fields that are excluded by projection #9 IslandRhythms

0.3.0 / 2021-12-20

  • feat: add Mongoose 6 to supported versions #8 medolino

0.2.2 / 2021-12-02

  • fix: fix compiling with strict tsconfig #7 Maks-s

0.2.1 / 2021-09-22

  • fix: upgrade to latest mpath #5

0.2.0 / 2021-03-03

  • feat: add index.d.ts for TypeScript support #4

0.1.2 / 2019-12-05

  • fix: only call getters once when using find() #1

0.1.1 / 2019-07-18

  • docs: add example to README