Skip to content

Commit 29354cb

Browse files
davimacedodplewis
authored andcommitted
Aggregate supports group by date fields (parse-community#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
1 parent 11c88ac commit 29354cb

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

spec/ParseQuery.Aggregate.spec.js

+51-11
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,23 @@ describe('Parse.Query Aggregate testing', () => {
379379
});
380380

381381
it_exclude_dbs(['postgres'])(
382-
'cannot group by date field (excluding createdAt and updatedAt)',
382+
'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
383383
done => {
384-
const obj1 = new TestObject({ dateField: new Date(1990, 11, 1) });
385-
const obj2 = new TestObject({ dateField: new Date(1990, 5, 1) });
386-
const obj3 = new TestObject({ dateField: new Date(1990, 11, 1) });
384+
const obj1 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
385+
const obj2 = new TestObject({ dateField2019: new Date(1990, 5, 1) });
386+
const obj3 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
387387
const pipeline = [
388+
{
389+
match: {
390+
dateField2019: { $exists: true },
391+
},
392+
},
388393
{
389394
group: {
390395
objectId: {
391-
day: { $dayOfMonth: '$dateField' },
392-
month: { $month: '$dateField' },
393-
year: { $year: '$dateField' },
396+
day: { $dayOfMonth: '$dateField2019' },
397+
month: { $month: '$dateField2019' },
398+
year: { $year: '$dateField2019' },
394399
},
395400
count: { $sum: 1 },
396401
},
@@ -401,11 +406,46 @@ describe('Parse.Query Aggregate testing', () => {
401406
const query = new Parse.Query(TestObject);
402407
return query.aggregate(pipeline);
403408
})
404-
.then(done.fail)
405-
.catch(error => {
406-
expect(error.code).toEqual(Parse.Error.INVALID_QUERY);
409+
.then(results => {
410+
const counts = results.map(result => result.count);
411+
expect(counts.length).toBe(2);
412+
expect(counts.sort()).toEqual([1, 2]);
407413
done();
408-
});
414+
})
415+
.catch(done.fail);
416+
}
417+
);
418+
419+
it_only_db('postgres')(
420+
'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
421+
done => {
422+
const obj1 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
423+
const obj2 = new TestObject({ dateField2019: new Date(1990, 5, 1) });
424+
const obj3 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
425+
const pipeline = [
426+
{
427+
group: {
428+
objectId: {
429+
day: { $dayOfMonth: '$dateField2019' },
430+
month: { $month: '$dateField2019' },
431+
year: { $year: '$dateField2019' },
432+
},
433+
count: { $sum: 1 },
434+
},
435+
},
436+
];
437+
Parse.Object.saveAll([obj1, obj2, obj3])
438+
.then(() => {
439+
const query = new Parse.Query(TestObject);
440+
return query.aggregate(pipeline);
441+
})
442+
.then(results => {
443+
const counts = results.map(result => result.count);
444+
expect(counts.length).toBe(3);
445+
expect(counts.sort()).toEqual([1, 2, 4]);
446+
done();
447+
})
448+
.catch(done.fail);
409449
}
410450
);
411451

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

-6
Original file line numberDiff line numberDiff line change
@@ -765,12 +765,6 @@ export class MongoStorageAdapter implements StorageAdapter {
765765
maxTimeMS: this._maxTimeMS,
766766
})
767767
)
768-
.catch(error => {
769-
if (error.code === 16006) {
770-
throw new Parse.Error(Parse.Error.INVALID_QUERY, error.message);
771-
}
772-
throw error;
773-
})
774768
.then(results => {
775769
results.forEach(result => {
776770
if (result.hasOwnProperty('_id')) {

0 commit comments

Comments
 (0)