mlv
mongoose-lean-virtuals
npm i mongoose-lean-virtuals
mlv

mongoose-lean-virtuals

Attach virtuals to the results of mongoose lean() queries

by Valeri Karpov

0.9.1 (see all)License:Apache 2.0TypeScript:Built-In
npm i mongoose-lean-virtuals
Readme

mongoose-lean-virtuals

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

Read the docs here.

Usage

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 });
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate