diff --git a/lib/queryhelpers.js b/lib/queryhelpers.js index 8ce3500ea70..9f6db1eba7c 100644 --- a/lib/queryhelpers.js +++ b/lib/queryhelpers.js @@ -3,6 +3,7 @@ * Module dependencies */ +var get = require('lodash.get'); var utils = require('./utils'); /*! @@ -75,7 +76,6 @@ exports.createModel = function createModel(model, doc, fields, userProvidedField exports.applyPaths = function applyPaths(fields, schema) { // determine if query is selecting or excluding fields - var exclude; var keys; var ki; @@ -123,8 +123,25 @@ exports.applyPaths = function applyPaths(fields, schema) { } // check for parent exclusions - var root = path.split('.')[0]; - if (~excluded.indexOf(root)) return; + var pieces = path.split('.'); + var root = pieces[0]; + if (~excluded.indexOf(root)) { + return; + } + + // Special case: if user has included a parent path of a discriminator key, + // don't explicitly project in the discriminator key because that will + // project out everything else under the parent path + if (!exclude && get(type, 'options.$skipDiscriminatorCheck', false)) { + var cur = ''; + for (var i = 0; i < pieces.length; ++i) { + cur += (cur.length === 0 ? '' : '.') + pieces[i]; + var projection = get(fields, cur, false); + if (projection && typeof projection !== 'object') { + return; + } + } + } (type.selected ? selected : excluded).push(path); };