From db994ed473164e5d779ac5d0125abde465854785 Mon Sep 17 00:00:00 2001 From: Antonio Davi Macedo Coelho de Castro Date: Fri, 26 Apr 2019 18:33:03 -0700 Subject: [PATCH] Aggregate supports group by date fields (#5538) * it actually supports group by date fields * Changing the field name again to see Travis logs * Adding match stage to the test * Adding test for group by date fields on postgres --- spec/ParseQuery.Aggregate.spec.js | 62 +++++++++++++++---- .../Storage/Mongo/MongoStorageAdapter.js | 6 -- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/spec/ParseQuery.Aggregate.spec.js b/spec/ParseQuery.Aggregate.spec.js index 40502de6..0383e468 100644 --- a/spec/ParseQuery.Aggregate.spec.js +++ b/spec/ParseQuery.Aggregate.spec.js @@ -379,18 +379,23 @@ describe('Parse.Query Aggregate testing', () => { }); it_exclude_dbs(['postgres'])( - 'cannot group by date field (excluding createdAt and updatedAt)', + 'can group by any date field (it does not work if you have dirty data)', // rows in your collection with non date data in the field that is supposed to be a date done => { - const obj1 = new TestObject({ dateField: new Date(1990, 11, 1) }); - const obj2 = new TestObject({ dateField: new Date(1990, 5, 1) }); - const obj3 = new TestObject({ dateField: new Date(1990, 11, 1) }); + const obj1 = new TestObject({ dateField2019: new Date(1990, 11, 1) }); + const obj2 = new TestObject({ dateField2019: new Date(1990, 5, 1) }); + const obj3 = new TestObject({ dateField2019: new Date(1990, 11, 1) }); const pipeline = [ + { + match: { + dateField2019: { $exists: true }, + }, + }, { group: { objectId: { - day: { $dayOfMonth: '$dateField' }, - month: { $month: '$dateField' }, - year: { $year: '$dateField' }, + day: { $dayOfMonth: '$dateField2019' }, + month: { $month: '$dateField2019' }, + year: { $year: '$dateField2019' }, }, count: { $sum: 1 }, }, @@ -401,11 +406,46 @@ describe('Parse.Query Aggregate testing', () => { const query = new Parse.Query(TestObject); return query.aggregate(pipeline); }) - .then(done.fail) - .catch(error => { - expect(error.code).toEqual(Parse.Error.INVALID_QUERY); + .then(results => { + const counts = results.map(result => result.count); + expect(counts.length).toBe(2); + expect(counts.sort()).toEqual([1, 2]); done(); - }); + }) + .catch(done.fail); + } + ); + + it_only_db('postgres')( + 'can group by any date field (it does not work if you have dirty data)', // rows in your collection with non date data in the field that is supposed to be a date + done => { + const obj1 = new TestObject({ dateField2019: new Date(1990, 11, 1) }); + const obj2 = new TestObject({ dateField2019: new Date(1990, 5, 1) }); + const obj3 = new TestObject({ dateField2019: new Date(1990, 11, 1) }); + const pipeline = [ + { + group: { + objectId: { + day: { $dayOfMonth: '$dateField2019' }, + month: { $month: '$dateField2019' }, + year: { $year: '$dateField2019' }, + }, + count: { $sum: 1 }, + }, + }, + ]; + Parse.Object.saveAll([obj1, obj2, obj3]) + .then(() => { + const query = new Parse.Query(TestObject); + return query.aggregate(pipeline); + }) + .then(results => { + const counts = results.map(result => result.count); + expect(counts.length).toBe(3); + expect(counts.sort()).toEqual([1, 2, 4]); + done(); + }) + .catch(done.fail); } ); diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index 31686846..0e74a832 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -765,12 +765,6 @@ export class MongoStorageAdapter implements StorageAdapter { maxTimeMS: this._maxTimeMS, }) ) - .catch(error => { - if (error.code === 16006) { - throw new Parse.Error(Parse.Error.INVALID_QUERY, error.message); - } - throw error; - }) .then(results => { results.forEach(result => { if (result.hasOwnProperty('_id')) {