diff --git a/app/models/server/models/_BaseDb.js b/app/models/server/models/_BaseDb.js index c88a29a90b3ac..87785f6a5ec23 100644 --- a/app/models/server/models/_BaseDb.js +++ b/app/models/server/models/_BaseDb.js @@ -125,26 +125,42 @@ export class BaseDb extends EventEmitter { } _ensureDefaultFields(options) { - if ((options?.fields == null || Object.keys(options?.fields).length === 0) && this.baseModel.defaultFields) { - options.fields = this.baseModel.defaultFields; + if (!this.baseModel.defaultFields) { + return options; } + + if (!options) { + return { fields: this.baseModel.defaultFields }; + } + + if (options.fields != null && Object.keys(options.fields).length > 0) { + return options; + } + + return { + ...options, + fields: this.baseModel.defaultFields, + }; } _doNotMixInclusionAndExclusionFields(options) { - this._ensureDefaultFields(options); + const optionsDef = this._ensureDefaultFields(options); + if (!optionsDef?.fields) { + return optionsDef; + } - if (options && options.fields) { - const keys = Object.keys(options.fields); - const removeKeys = keys.filter((key) => options.fields[key] === 0); - if (keys.length > removeKeys.length) { - removeKeys.forEach((key) => delete options.fields[key]); - } + const keys = Object.keys(optionsDef.fields); + const removeKeys = keys.filter((key) => optionsDef.fields[key] === 0); + if (keys.length > removeKeys.length) { + removeKeys.forEach((key) => delete optionsDef.fields[key]); } + + return optionsDef; } find(query = {}, options = {}) { - this._doNotMixInclusionAndExclusionFields(options); - return this.model.find(query, options); + const optionsDef = this._doNotMixInclusionAndExclusionFields(options); + return this.model.find(query, optionsDef); } findById(_id, options) { @@ -152,8 +168,8 @@ export class BaseDb extends EventEmitter { } findOne(query = {}, options = {}) { - this._doNotMixInclusionAndExclusionFields(options); - return this.model.findOne(query, options); + const optionsDef = this._doNotMixInclusionAndExclusionFields(options); + return this.model.findOne(query, optionsDef); } findOneById(_id, options) { diff --git a/app/models/server/raw/BaseRaw.js b/app/models/server/raw/BaseRaw.js index 7d126d26797d0..312ff9df2444b 100644 --- a/app/models/server/raw/BaseRaw.js +++ b/app/models/server/raw/BaseRaw.js @@ -4,9 +4,24 @@ export class BaseRaw { } _ensureDefaultFields(options) { - if ((options?.fields == null || Object.keys(options?.fields).length === 0) && this.defaultFields) { - options.fields = this.defaultFields; + if (!this.defaultFields) { + return options; } + + if (!options) { + return { projection: this.defaultFields }; + } + + // TODO: change all places using "fields" for raw models and remove the additional condition here + if ((options.projection != null && Object.keys(options.projection).length > 0) + || (options.fields != null && Object.keys(options.fields).length > 0)) { + return options; + } + + return { + ...options, + projection: this.defaultFields, + }; } findOneById(_id, options = {}) { @@ -14,8 +29,8 @@ export class BaseRaw { } findOne(query = {}, options = {}) { - this._ensureDefaultFields(options); - return this.col.findOne(query, options); + const optionsDef = this._ensureDefaultFields(options); + return this.col.findOne(query, optionsDef); } findUsersInRoles() { @@ -23,8 +38,8 @@ export class BaseRaw { } find(query = {}, options = {}) { - this._ensureDefaultFields(options); - return this.col.find(query, options); + const optionsDef = this._ensureDefaultFields(options); + return this.col.find(query, optionsDef); } update(...args) {