Skip to content

Commit c4ed01b

Browse files
authored
Fix aggregate group id (parse-community#5994)
* Fix aggregate group id * Improve implementation * Add test case * Fix postgres test - it does not work with group and sort at same time
1 parent 3bb0517 commit c4ed01b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,29 @@ describe('Parse.Query Aggregate testing', () => {
285285
});
286286
});
287287

288+
it('group by number', done => {
289+
const options = Object.assign({}, masterKeyOptions, {
290+
body: {
291+
group: { objectId: '$score' },
292+
},
293+
});
294+
get(Parse.serverURL + '/aggregate/TestObject', options)
295+
.then(resp => {
296+
expect(resp.results.length).toBe(2);
297+
expect(
298+
Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')
299+
).toBe(true);
300+
expect(
301+
Object.prototype.hasOwnProperty.call(resp.results[1], 'objectId')
302+
).toBe(true);
303+
expect(
304+
resp.results.sort((a, b) => (a.objectId > b.objectId ? 1 : -1))
305+
).toEqual([{ objectId: 10 }, { objectId: 20 }]);
306+
done();
307+
})
308+
.catch(done.fail);
309+
});
310+
288311
it_exclude_dbs(['postgres'])('group and multiply transform', done => {
289312
const obj1 = new TestObject({ name: 'item a', quantity: 2, price: 10 });
290313
const obj2 = new TestObject({ name: 'item b', quantity: 5, price: 5 });

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,12 @@ export class MongoStorageAdapter implements StorageAdapter {
799799
if (isPointerField && result._id) {
800800
result._id = result._id.split('$')[1];
801801
}
802-
if (result._id == null || _.isEmpty(result._id)) {
802+
if (
803+
result._id == null ||
804+
result._id == undefined ||
805+
(['object', 'string'].includes(typeof result._id) &&
806+
_.isEmpty(result._id))
807+
) {
803808
result._id = null;
804809
}
805810
result.objectId = result._id;

0 commit comments

Comments
 (0)