diff --git a/packages/strapi-connector-mongoose/lib/buildQuery.js b/packages/strapi-connector-mongoose/lib/buildQuery.js index d7e95ccf1d..e04d2ec029 100644 --- a/packages/strapi-connector-mongoose/lib/buildQuery.js +++ b/packages/strapi-connector-mongoose/lib/buildQuery.js @@ -1,3 +1,5 @@ +'use strict'; + const _ = require('lodash'); const utils = require('./utils')(); @@ -294,6 +296,15 @@ const buildLookupMatch = ({ assoc }) => { }, }; } + case 'manyWay': { + return { + $match: { + $expr: { + $in: ['$_id', '$$localAlias'], + }, + }, + }; + } case 'manyToMany': { if (assoc.dominant === true) { return { diff --git a/packages/strapi-connector-mongoose/lib/utils/index.js b/packages/strapi-connector-mongoose/lib/utils/index.js index 9d62abece4..ecc80df69e 100644 --- a/packages/strapi-connector-mongoose/lib/utils/index.js +++ b/packages/strapi-connector-mongoose/lib/utils/index.js @@ -100,6 +100,8 @@ module.exports = (mongoose = Mongoose) => { }; const valueToId = value => { + if (Array.isArray(value)) return value.map(valueToId); + if (isMongoId(value)) { return mongoose.Types.ObjectId(value); } diff --git a/packages/strapi-utils/lib/buildQuery.js b/packages/strapi-utils/lib/buildQuery.js index 5e814b3323..219796c537 100644 --- a/packages/strapi-utils/lib/buildQuery.js +++ b/packages/strapi-utils/lib/buildQuery.js @@ -17,7 +17,7 @@ const isAttribute = (model, field) => * Returns the model, attribute name and association from a path of relation * @param {Object} options - Options * @param {string} options.model - Strapi model - * @param {string} options.field - pathj of relation / attribute + * @param {string} options.field - path of relation / attribute */ const getAssociationFromFieldKey = ({ model, field }) => { const fieldParts = field.split('.'); @@ -82,6 +82,22 @@ const castValue = ({ type, value, operator }) => { if (operator === 'null') return parseType({ type: 'boolean', value }); return parseType({ type, value }); }; + +/** + * + * @param {Object} options - Options + * @param {string} options.model - The model + * @param {string} options.field - path of relation / attribute + */ +const normalizeFieldName = ({ model, field }) => { + const fieldPath = field.split('.'); + return _.last(fieldPath) === 'id' + ? _.initial(fieldPath) + .concat(model.primaryKey) + .join('.') + : fieldPath.join('.'); +}; + /** * * @param {Object} options - Options @@ -116,7 +132,7 @@ const buildQuery = ({ model, filters = {}, ...rest }) => { const castedValue = castInput({ type, operator, value }); return { - field: field === 'id' ? model.primaryKey : field, + field: normalizeFieldName({ model, field }), operator, value: castedValue, };