diff --git a/models/profile.js b/models/profile.js index 0470b9a..d4eb621 100644 --- a/models/profile.js +++ b/models/profile.js @@ -169,26 +169,20 @@ module.exports = { allMessageImages: (e, profileId) => { const promise = new Promise((resolve, reject) => { ProfileModel - .findById(profileId, (err, profile) => { - if (err) { - reject(err); - } - - if (profile) { - profile.messages - .find({ 'image': { $exists: true } }) - .select('image -_id') - .exec((err, result) => { - if (err) { - reject(err); - } - - if (result) { - resolve(result); - } - }); - } - }); + .aggregate( + { $match: { _id: profileId } }, + { $unwind: '$messages' }, + { $match: { 'messages.image': { $exists: true } } } + ) + .exec((err, result) => { + if (err) { + reject(err); + } + + if (result) { + resolve(result); + } + }); }); promise.then((result) => {