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
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-04-26 18:33:03 -07:00
committed by Diamond Lewis
parent 43c41925a2
commit db994ed473
2 changed files with 51 additions and 17 deletions

View File

@@ -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);
}
);

View File

@@ -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')) {