https://github.com/mongoosejs/mongoose-lean-getters
Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
https://github.com/mongoosejs/mongoose-lean-getters
Last synced: 8 months ago
JSON representation
Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
- Host: GitHub
- URL: https://github.com/mongoosejs/mongoose-lean-getters
- Owner: mongoosejs
- License: apache-2.0
- Created: 2019-07-17T18:32:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-24T21:38:55.000Z (10 months ago)
- Last Synced: 2025-03-31T12:07:54.467Z (9 months ago)
- Language: JavaScript
- Size: 83 KB
- Stars: 10
- Watchers: 2
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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
```javascript
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:
```javascript
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'
```