Mongoose Plugins
- Usage
- examples
- Changelog
- 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-12
- 0.7.1 / 2020-10-09
- 0.7.0 / 2020-10-06
- 0.6.9 / 2020-08-29
- 0.6.8 / 2020-06-11
- 0.6.7 / 2020-06-04
- 0.6.6 / 2020-06-03
- 0.6.5 / 2020-06-02
- 0.6.4 / 2020-06-02
- 0.6.3 / 2020-05-24
- 0.6.2 / 2020-04-27
- 0.6.1 / 2020-03-17
- 0.6.0 / 2020-03-04
- 0.5.0 / 2019-10-16
- 0.4.4 / 2019-09-23
- 0.4.3 / 2019-06-03
- 0.4.2 / 2019-05-09
- 0.4.1 / 2019-05-07
- 0.4.0 / 2019-04-21
- 0.3.5 / 2019-03-11
- 0.3.4 / 2018-11-13
- 0.3.3 / 2018-11-09
- 0.3.2 / 2018-10-23
- 0.3.1 / 2018-10-10
- 0.3.0 / 2018-01-29
mongoose-lean-virtuals
Mongoose Plugins
- Usage
- examples
- Changelog
- 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-12
- 0.7.1 / 2020-10-09
- 0.7.0 / 2020-10-06
- 0.6.9 / 2020-08-29
- 0.6.8 / 2020-06-11
- 0.6.7 / 2020-06-04
- 0.6.6 / 2020-06-03
- 0.6.5 / 2020-06-02
- 0.6.4 / 2020-06-02
- 0.6.3 / 2020-05-24
- 0.6.2 / 2020-04-27
- 0.6.1 / 2020-03-17
- 0.6.0 / 2020-03-04
- 0.5.0 / 2019-10-16
- 0.4.4 / 2019-09-23
- 0.4.3 / 2019-06-03
- 0.4.2 / 2019-05-09
- 0.4.1 / 2019-05-07
- 0.4.0 / 2019-04-21
- 0.3.5 / 2019-03-11
- 0.3.4 / 2018-11-13
- 0.3.3 / 2018-11-09
- 0.3.2 / 2018-10-23
- 0.3.1 / 2018-10-10
- 0.3.0 / 2018-01-29
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 });
examples
It attaches virtuals to result of find, findOne, findById, findByIdAndUpdate, and findOneAndUpdate if lean
const schema = new mongoose.Schema({
name: String
});
schema.virtual('lowercase').get(function() {
return this.name.toLowerCase();
});
schema.plugin(mongooseLeanVirtuals);
const Model = mongoose.model('Test', schema);
return Model.create({ name: 'Val' }).
then(() => Promise.all([
// You **must** pass `virtuals: true` to `lean()`
Model.find().lean({ virtuals: true }),
Model.findOne().lean({ virtuals: true }),
Model.findOneAndUpdate({}, { name: 'VAL' }).lean({ virtuals: true })
])).
then(results => {
const findRes = results[0];
const findOneRes = results[1];
const findOneAndUpdateRes = results[2];
assert.equal(findRes[0].lowercase, 'val');
assert.equal(findOneRes.lowercase, 'val');
assert.equal(findOneAndUpdateRes.lowercase, 'val');
// Mongoose has an `id` virtual by default that gets the `_id` as a
// string.
assert.equal(findRes[0].id, findRes[0]._id.toString());
assert.equal(findOneRes.id, findOneRes._id.toString());
assert.equal(findOneAndUpdateRes.id, findOneAndUpdateRes._id.toString());
});
It lets you choose which virtuals to apply
If you specify a list of virtuals in lean()
, this plugin will only
apply those virtuals. This lets you pick which virtuals show up.
const schema = new mongoose.Schema({
name: String
}, { id: false });
schema.virtual('lowercase').get(function() {
return this.name.toLowerCase();
});
schema.virtual('uppercase').get(function() {
return this.name.toUpperCase();
});
schema.plugin(mongooseLeanVirtuals);
const Model = mongoose.model('Test2', schema);
return Model.create({ name: 'Val' }).
then(() => Model.findOne().lean({ virtuals: ['uppercase'] })).
then(result => {
assert.equal(result.uppercase, 'VAL');
assert.ok(!result.lowercase);
});
It lets you access a lean subdocument's parent
Accessing the parent document is tricky because lean documents don't
have a parent()
method or any other Mongoose-specific functionality.
To support that use case, this plugin exports a parent()
function that
lets you get a document's parent.
const childSchema = new mongoose.Schema({ firstName: String });
childSchema.virtual('fullName').get(function() {
if (this instanceof mongoose.Document) {
return `${this.firstName} ${this.parent().lastName}`;
}
// This `fullName` virtual is in a subdocument, so in order to get the
// parent's `lastName` you need to use this plugin's `parent()` function.
return `${this.firstName} ${mongooseLeanVirtuals.parent(this).lastName}`;
});
const parentSchema = new mongoose.Schema({
firstName: String,
lastName: String,
child: childSchema
});
parentSchema.plugin(mongooseLeanVirtuals);
const Parent = mongoose.model('Parent', parentSchema);
const doc = {
firstName: 'Anakin',
lastName: 'Skywalker',
child: { firstName: 'Luke' }
};
return Parent.create(doc).
then(() => Parent.findOne().lean({ virtuals: true })).
then(result => {
assert.equal(result.child.fullName, 'Luke Skywalker');
});
Changelog
0.8.0 / 2021-04-26
- fix: handle calling
parent()
in virtual when usingfind()
with multiple results #51 - fix: require Mongoose >= 5.11.10 for fix to #48
0.7.6 / 2020-12-15
- fix: propagate
virtuals: true
to subdocuments #47 #43
0.7.5 / 2020-11-28
- fix: fix .length invalid property access on null #44 maximilianschmid
0.7.4 / 2020-11-20
- fix: support nested virtuals #43 rdougan
0.7.3 / 2020-11-12
- fix: skip non-existent virtuals when passing a list of virtual names to
lean()
#42
0.7.2 / 2020-10-12
- fix: make
parent()
tracking support case where array of subdocs contains primitives #41
0.7.1 / 2020-10-09
- fix: avoid WeakMap error when using arrays with
null
elements #41
0.7.0 / 2020-10-06
- feat: add top-level
parent()
function that lets you get the subdocument's parent even though the subdoc is lean #40
0.6.9 / 2020-08-29
- fix: avoid TypeError when there are multiple discriminators #39
0.6.8 / 2020-06-11
- fix: apply virtuals in doubly nested document arrays #38
0.6.7 / 2020-06-04
- fix build for node v6 and v4
0.6.6 / 2020-06-03
- fix: discriminators when the query result is an array #37 #36 FERNman
0.6.5 / 2020-06-02
- fix: avoid infinite recursion on recursive schemas with virtuals #33
0.6.4 / 2020-06-02
- fix: allow explicitly selecting subdocument virtuals #35 #34 ChrisLahaye
0.6.3 / 2020-05-24
- fix: skip checking discriminators if result is null #32
0.6.2 / 2020-04-27
- fix: correctly pass existing field value to applyGetters #31 makinde
0.6.1 / 2020-03-17
- fix: get virtuals from discriminator schema if discriminator key set #30 makinde
0.6.0 / 2020-03-04
- feat: attach lean virtuals to result of
findOneAndRemove()
andfindOneAndDelete()
#29 isaacdecoded
0.5.0 / 2019-10-16
- fix: use post order traversal so child schema virtuals are set before parent schema #28
0.4.4 / 2019-09-23
- fix: check for empty path #26 thoglen
0.4.3 / 2019-06-03
- fix: avoid trying to virtualize undefined doc #24 AlexandreGymlib
0.4.2 / 2019-05-09
- fix: handle virtuals in nested schemas with find() #22
0.4.1 / 2019-05-07
- fix: support Mongoose 5.x cursors #21
0.4.0 / 2019-04-21
- feat: support virtuals in nested schemas #20
0.3.5 / 2019-03-11
- fix: support
ref
in virtual options #19 linusbrolin
0.3.4 / 2018-11-13
- fix: attach all virtuals as opposed to just one #14 artemkobets
0.3.3 / 2018-11-09
- fix: fix one more issue with
eachAsync()
#12 nico29
0.3.2 / 2018-10-23
- fix: support Mongoose cursor's
eachAsync()
#9
0.3.1 / 2018-10-10
- docs: link to new docs site on plugins.mongoosejs.io
0.3.0 / 2018-01-29
- fix: delay checking virtuals until the middleware for 5.0 support #6