Skip to content

Commit

Permalink
fix(populate): handle slice projections correctly when automatically …
Browse files Browse the repository at this point in the history
…selecting populated fields

Fix #5737
  • Loading branch information
vkarpov15 committed Oct 27, 2017
1 parent f02641c commit 1d87987
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var mquery = require('mquery');
var readPref = require('./drivers').ReadPreference;
var selectPopulatedFields = require('./services/query/selectPopulatedFields');
var setDefaultsOnInsert = require('./services/setDefaultsOnInsert');
var slice = require('sliced');
var updateValidators = require('./services/updateValidators');
var util = require('util');
var utils = require('./utils');
Expand Down Expand Up @@ -263,6 +264,49 @@ Query.prototype.toConstructor = function toConstructor() {
* @api public
*/

Query.prototype.slice = function() {
if (arguments.length === 0) {
return this;
}

this._validate('slice');

var path;
var val;

if (arguments.length === 1) {
var arg = arguments[0];
if (typeof arg === 'object' && !Array.isArray(arg)) {
var keys = Object.keys(arg);
var numKeys = keys.length;
for (var i = 0; i < numKeys; ++i) {
this.slice(keys[i], arg[keys[i]]);
}
return this;
}
this._ensurePath('slice');
path = this._path;
val = arguments[0];
} else if (arguments.length === 2) {
if ('number' === typeof arguments[0]) {
this._ensurePath('slice');
path = this._path;
val = slice(arguments);
} else {
path = arguments[0];
val = arguments[1];
}
} else if (arguments.length === 3) {
path = arguments[0];
val = slice(arguments, 1);
}

var p = {};
p[path] = { $slice: val };
return this.select(p);
};


/**
* Specifies the complementary comparison value for paths specified with `where()`
*
Expand Down
2 changes: 1 addition & 1 deletion lib/services/query/selectPopulatedFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ function isPathInFields(userProvidedFields, path) {
}
cur += '.' + pieces[i];
}
return false;
return userProvidedFields[cur] != null;
}

3 comments on commit 1d87987

@aforty
Copy link

@aforty aforty commented on 1d87987 Oct 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add the test provided in #5737 to mitigate future regression?

@vkarpov15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually put tests in separate commits: f02641c because that makes it easier to git stash and verify that the test fails without the fix.

@aforty
Copy link

@aforty aforty commented on 1d87987 Oct 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good strategy. Thanks again, looks great on my end.

Please sign in to comment.