fix mongo preview when array is missing (#8887)

This commit is contained in:
Pierre Noël 2020-12-14 12:01:27 +01:00 committed by GitHub
parent 7fa67d8f62
commit f8f7a4a89f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -412,7 +412,7 @@ const buildLookupMatch = ({ assoc, assocModel, filters = {} }) => {
$match: { $match: {
$and: defaultMatches.concat({ $and: defaultMatches.concat({
$expr: { $expr: {
$in: ['$_id', '$$localAlias'], $in: ['$_id', { $ifNull: ['$$localAlias', []] }],
}, },
}), }),
}, },
@ -424,7 +424,7 @@ const buildLookupMatch = ({ assoc, assocModel, filters = {} }) => {
$match: { $match: {
$and: defaultMatches.concat({ $and: defaultMatches.concat({
$expr: { $expr: {
$in: ['$_id', '$$localAlias'], $in: ['$_id', { $ifNull: ['$$localAlias', []] }],
}, },
}), }),
}, },
@ -435,7 +435,7 @@ const buildLookupMatch = ({ assoc, assocModel, filters = {} }) => {
$match: { $match: {
$and: defaultMatches.concat({ $and: defaultMatches.concat({
$expr: { $expr: {
$in: ['$$localId', `$${assoc.via}`], $in: ['$$localId', { $ifNull: [`$${assoc.via}`, []] }],
}, },
}), }),
}, },

View File

@ -550,7 +550,11 @@ module.exports = ({ model, strapi }) => {
return model return model
.aggregate() .aggregate()
.match({ [model.primaryKey]: { $in: entitiesIds.map(mongoose.Types.ObjectId) } }) .match({ [model.primaryKey]: { $in: entitiesIds.map(mongoose.Types.ObjectId) } })
.project({ _id: 0, id: '$_id', count: { $size: `$${assoc.alias}` } }); .project({
_id: 0,
id: '$_id',
count: { $size: { $ifNull: [`$${assoc.alias}`, []] } },
});
} }
const assocModel = strapi.db.getModelByAssoc(assoc); const assocModel = strapi.db.getModelByAssoc(assoc);
return assocModel return assocModel