Support pointer in aggregate query (#4493)
This commit is contained in:
@@ -96,6 +96,27 @@ describe('Parse.Query Aggregate testing', () => {
|
|||||||
}).catch(done.fail);
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('group by pointer', (done) => {
|
||||||
|
const pointer1 = new TestObject();
|
||||||
|
const pointer2 = new TestObject();
|
||||||
|
const obj1 = new TestObject({ pointer: pointer1 });
|
||||||
|
const obj2 = new TestObject({ pointer: pointer2 });
|
||||||
|
const obj3 = new TestObject({ pointer: pointer1 });
|
||||||
|
const pipeline = [
|
||||||
|
{ group: { objectId: '$pointer' } }
|
||||||
|
];
|
||||||
|
Parse.Object.saveAll([pointer1, pointer2, obj1, obj2, obj3]).then(() => {
|
||||||
|
const query = new Parse.Query(TestObject);
|
||||||
|
return query.aggregate(pipeline);
|
||||||
|
}).then((results) => {
|
||||||
|
expect(results.length).toEqual(3);
|
||||||
|
expect(results.some(result => result.objectId === pointer1.id)).toEqual(true);
|
||||||
|
expect(results.some(result => result.objectId === pointer2.id)).toEqual(true);
|
||||||
|
expect(results.some(result => result.objectId === null)).toEqual(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('group sum query', (done) => {
|
it('group sum query', (done) => {
|
||||||
const options = Object.assign({}, masterKeyOptions, {
|
const options = Object.assign({}, masterKeyOptions, {
|
||||||
body: {
|
body: {
|
||||||
|
|||||||
@@ -515,12 +515,26 @@ export class MongoStorageAdapter implements StorageAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
aggregate(className: string, schema: any, pipeline: any, readPreference: ?string) {
|
aggregate(className: string, schema: any, pipeline: any, readPreference: ?string) {
|
||||||
|
let isPointerField = false;
|
||||||
|
pipeline = pipeline.map((stage) => {
|
||||||
|
if (stage.$group && stage.$group._id) {
|
||||||
|
const field = stage.$group._id.substring(1);
|
||||||
|
if (schema.fields[field] && schema.fields[field].type === 'Pointer') {
|
||||||
|
isPointerField = true;
|
||||||
|
stage.$group._id = `$_p_${field}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stage;
|
||||||
|
});
|
||||||
readPreference = this._parseReadPreference(readPreference);
|
readPreference = this._parseReadPreference(readPreference);
|
||||||
return this._adaptiveCollection(className)
|
return this._adaptiveCollection(className)
|
||||||
.then(collection => collection.aggregate(pipeline, { readPreference, maxTimeMS: this._maxTimeMS }))
|
.then(collection => collection.aggregate(pipeline, { readPreference, maxTimeMS: this._maxTimeMS }))
|
||||||
.then(results => {
|
.then(results => {
|
||||||
results.forEach(result => {
|
results.forEach(result => {
|
||||||
if (result.hasOwnProperty('_id')) {
|
if (result.hasOwnProperty('_id')) {
|
||||||
|
if (isPointerField && result._id) {
|
||||||
|
result._id = result._id.split('$')[1];
|
||||||
|
}
|
||||||
result.objectId = result._id;
|
result.objectId = result._id;
|
||||||
delete result._id;
|
delete result._id;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user