Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/helpers/projection/parseProjection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';

/**
* Convert a string or array into a projection object, retaining all
* `-` and `+` paths.
* Convert a string or array into a projection object. Treats `-foo` as
* equivalent to `foo: 0` depending on `retainMinusPaths`. If `retainMinusPaths`
* is true, then `-foo` will be included in the projection as `'-foo': 0`.
*
* @param {object|string|string[]} v
* @param {boolean} [retainMinusPaths]
* @return {object}
*/

module.exports = function parseProjection(v, retainMinusPaths) {
Expand All @@ -22,9 +27,9 @@ module.exports = function parseProjection(v, retainMinusPaths) {
if (!field) {
continue;
}
const include = '-' == field[0] ? 0 : 1;
const include = field.charAt(0) === '-' ? 0 : 1;
if (!retainMinusPaths && include === 0) {
field = field.substring(1);
field = field.slice(1);
}
ret[field] = include;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const decorateDiscriminatorIndexOptions = require('./helpers/indexes/decorateDis
const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
const leanPopulateMap = require('./helpers/populate/leanPopulateMap');
const parallelLimit = require('./helpers/parallelLimit');
const parseProjection = require('./helpers/projection/parseProjection');
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
const pushNestedArrayPaths = require('./helpers/model/pushNestedArrayPaths');
const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField');
Expand Down Expand Up @@ -3935,7 +3936,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
* @api public
*/

Model.hydrate = function(obj, projection, options) {
Model.hydrate = function hydrate(obj, projection, options) {
_checkContext(this, 'hydrate');

if (options?.virtuals && options?.hydratedPopulatedDocs === false) {
Expand All @@ -3946,6 +3947,7 @@ Model.hydrate = function(obj, projection, options) {
if (obj?.$__ != null) {
obj = obj.toObject(internalToObjectOptions);
}
projection = parseProjection(projection);
obj = applyProjection(obj, projection);
}
const document = require('./queryHelpers').createModel(this, obj, projection, projection, options);
Expand Down
19 changes: 17 additions & 2 deletions test/model.hydrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,33 @@ describe('model', function() {
assert.deepEqual(['food'], Object.keys(err.errors));
});

it('supports projection (gh-9209)', function() {
it('supports projection (gh-16082) (gh-9209)', function() {
const schema = new Schema({
prop: String,
arr: [String]
});
const Model = db.model('Test2', schema);

const doc = Model.hydrate({ prop: 'test' }, { arr: 0 });
let doc = Model.hydrate({ prop: 'test' }, { arr: 0 });
assert.equal(doc.isNew, false);
assert.equal(doc.isModified(), false);
assert.ok(!doc.$__delta());
assert.strictEqual(doc.prop, 'test');
// Array implicit default of `[]` shouldn't apply
assert.strictEqual(doc.arr, undefined);

doc = Model.hydrate({ prop: 'test' }, '-arr');
assert.equal(doc.isNew, false);
assert.equal(doc.isModified(), false);
assert.ok(!doc.$__delta());
assert.strictEqual(doc.prop, 'test');
assert.strictEqual(doc.arr, undefined);

doc = Model.hydrate({ prop: 'test' }, ['_id']);
assert.equal(doc.isNew, false);
assert.equal(doc.isModified(), false);
assert.ok(!doc.$__delta());
assert.ok(!doc.prop);
});

it('works correctly with model discriminators', function() {
Expand Down
Loading