Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion integration/test/ParseQueryAggregateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Parse Aggregate Query', () => {

it('aggregate pipeline object query', (done) => {
const pipeline = {
group: { objectId: '$name' }
group: { objectId: '$name' },
};
const query = new Parse.Query(TestObject);
query.aggregate(pipeline).then((results) => {
Expand Down Expand Up @@ -55,6 +55,47 @@ describe('Parse Aggregate Query', () => {
}
});

it('aggregate allow multiple of same stage', async () => {
const pointer1 = new TestObject({ value: 1 });
const pointer2 = new TestObject({ value: 2 });
const pointer3 = new TestObject({ value: 3 });

const obj1 = new TestObject({ pointer: pointer1, name: 'Hello' });
const obj2 = new TestObject({ pointer: pointer2, name: 'Hello' });
const obj3 = new TestObject({ pointer: pointer3, name: 'World' });

const pipeline = [{
match: { name: 'Hello' },
}, {
// Transform className$objectId to objectId and store in new field tempPointer
project: {
tempPointer: { $substr: ['$_p_pointer', 11, -1] }, // Remove TestObject$
},
}, {
// Left Join, replace objectId stored in tempPointer with an actual object
lookup: {
from: 'TestObject',
localField: 'tempPointer',
foreignField: '_id',
as: 'tempPointer',
},
}, {
// lookup returns an array, Deconstructs an array field to objects
unwind: {
path: '$tempPointer',
},
}, {
match: { 'tempPointer.value': 2 },
}];
await Parse.Object.saveAll([pointer1, pointer2, pointer3, obj1, obj2, obj3]);

const query = new Parse.Query(TestObject);
const results = await query.aggregate(pipeline);

expect(results.length).toEqual(1);
expect(results[0].tempPointer.value).toEqual(2);
});

it('distinct query', () => {
const query = new Parse.Query(TestObject);
return query.distinct('score').then((results) => {
Expand Down
19 changes: 6 additions & 13 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,30 +572,23 @@ class ParseQuery {
options = options || {};

const aggregateOptions = {
useMasterKey: true
useMasterKey: true,
};
if (options.hasOwnProperty('sessionToken')) {
aggregateOptions.sessionToken = options.sessionToken;
}
const controller = CoreManager.getQueryController();
let stages = {};

if (Array.isArray(pipeline)) {
pipeline.forEach((stage) => {
for (let op in stage) {
stages[op] = stage[op];
}
});
} else if (pipeline && typeof pipeline === 'object') {
stages = pipeline;
} else {
if (!Array.isArray(pipeline) && typeof pipeline !== 'object') {
throw new Error('Invalid pipeline must be Array or Object');
}

const params = { pipeline };

return controller.aggregate(
this.className,
stages,
aggregateOptions
params,
aggregateOptions,
).then((results) => {
return results.results;
});
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ describe('ParseQuery', () => {
aggregate(className, params, options) {
expect(className).toBe('Item');
expect(params).toEqual({
group: { objectId: '$name' }
pipeline: [{ group: { objectId: '$name' } }]
});
expect(options).toEqual({ useMasterKey: true });
return Promise.resolve({
Expand All @@ -1880,7 +1880,7 @@ describe('ParseQuery', () => {
aggregate(className, params, options) {
expect(className).toBe('Item');
expect(params).toEqual({
group: { objectId: '$name' }
pipeline: { group: { objectId: '$name' } }
});
expect(options).toEqual({ useMasterKey: true });
return Promise.resolve({
Expand Down Expand Up @@ -1929,7 +1929,7 @@ describe('ParseQuery', () => {
aggregate(className, params, options) {
expect(className).toBe('Item');
expect(params).toEqual({
group: { objectId: '$name' }
pipeline: [{ group: { objectId: '$name' } }]
});
expect(options).toEqual({
useMasterKey: true,
Expand Down