Skip to content

Commit 1d87987

Browse files
committed
fix(populate): handle slice projections correctly when automatically selecting populated fields
Fix #5737
1 parent f02641c commit 1d87987

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/query.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var mquery = require('mquery');
1515
var readPref = require('./drivers').ReadPreference;
1616
var selectPopulatedFields = require('./services/query/selectPopulatedFields');
1717
var setDefaultsOnInsert = require('./services/setDefaultsOnInsert');
18+
var slice = require('sliced');
1819
var updateValidators = require('./services/updateValidators');
1920
var util = require('util');
2021
var utils = require('./utils');
@@ -263,6 +264,49 @@ Query.prototype.toConstructor = function toConstructor() {
263264
* @api public
264265
*/
265266

267+
Query.prototype.slice = function() {
268+
if (arguments.length === 0) {
269+
return this;
270+
}
271+
272+
this._validate('slice');
273+
274+
var path;
275+
var val;
276+
277+
if (arguments.length === 1) {
278+
var arg = arguments[0];
279+
if (typeof arg === 'object' && !Array.isArray(arg)) {
280+
var keys = Object.keys(arg);
281+
var numKeys = keys.length;
282+
for (var i = 0; i < numKeys; ++i) {
283+
this.slice(keys[i], arg[keys[i]]);
284+
}
285+
return this;
286+
}
287+
this._ensurePath('slice');
288+
path = this._path;
289+
val = arguments[0];
290+
} else if (arguments.length === 2) {
291+
if ('number' === typeof arguments[0]) {
292+
this._ensurePath('slice');
293+
path = this._path;
294+
val = slice(arguments);
295+
} else {
296+
path = arguments[0];
297+
val = arguments[1];
298+
}
299+
} else if (arguments.length === 3) {
300+
path = arguments[0];
301+
val = slice(arguments, 1);
302+
}
303+
304+
var p = {};
305+
p[path] = { $slice: val };
306+
return this.select(p);
307+
};
308+
309+
266310
/**
267311
* Specifies the complementary comparison value for paths specified with `where()`
268312
*

lib/services/query/selectPopulatedFields.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ function isPathInFields(userProvidedFields, path) {
4141
}
4242
cur += '.' + pieces[i];
4343
}
44-
return false;
44+
return userProvidedFields[cur] != null;
4545
}

0 commit comments

Comments
 (0)