Skip to content

Commit

Permalink
albumSearch returns total number of results
Browse files Browse the repository at this point in the history
  • Loading branch information
drillprop committed Jun 9, 2019
1 parent 97e683e commit 315d7cc
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/graphql/resolvers/agregateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Types } from 'mongoose';

export const reduceToObject = aggregateResult => {
if (!aggregateResult.length) throw new Error('No albums found');
console.log(aggregateResult);
const albums = aggregateResult[0].albums.map(album => ({
...album,
id: album._id.toString()
Expand All @@ -16,23 +17,7 @@ export const reduceToObject = aggregateResult => {
export const albumSearch = (userId, search) => {
return [
{ $match: { _id: Types.ObjectId(userId) } },
{
$project: {
albums: 1,
albumsTotal: { $size: '$albums' }
}
},
{ $unwind: '$albums' },
{
$project: {
date: 0,
email: 0,
name: 0,
password: 0,
_id: 0,
__v: 0
}
},
{ $sort: { 'albums._id': -1 } },
{
$match: {
Expand All @@ -52,6 +37,19 @@ export const albumSearch = (userId, search) => {
]
}
},
{
$group: {
_id: null,
albums: { $push: '$albums' }
}
},
{
$project: {
albums: 1,
albumsTotal: { $size: '$albums' }
}
},
{ $unwind: '$albums' },
{
$group: {
_id: null,
Expand Down

0 comments on commit 315d7cc

Please sign in to comment.