From 315d7ccb2a06dc5e9aefd12c7b942d4961efd11e Mon Sep 17 00:00:00 2001 From: drillprops Date: Sun, 9 Jun 2019 22:05:20 +0200 Subject: [PATCH] albumSearch returns total number of results --- src/graphql/resolvers/agregateFunctions.js | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/graphql/resolvers/agregateFunctions.js b/src/graphql/resolvers/agregateFunctions.js index 5b53d3f..b393e9a 100644 --- a/src/graphql/resolvers/agregateFunctions.js +++ b/src/graphql/resolvers/agregateFunctions.js @@ -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() @@ -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: { @@ -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,