diff --git a/CHANGELOG.md b/CHANGELOG.md index 67b1e053a8..3bb7fe55c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ #### Improvements: * Fixes issue that would prevent users with large number of roles to resolve all of them [@Moumouls]() (#5131, #5132) - +* Fixes distinct query on special fields ([#5144](https://github.com/parse-community/parse-server/pull/5144)) ### 3.1.0 [Full Changelog](https://github.com/parse-community/parse-server/compare/3.0.0...3.1.0) diff --git a/spec/ParseQuery.Aggregate.spec.js b/spec/ParseQuery.Aggregate.spec.js index b45aa8e7ef..40502de696 100644 --- a/spec/ParseQuery.Aggregate.spec.js +++ b/spec/ParseQuery.Aggregate.spec.js @@ -1098,6 +1098,36 @@ describe('Parse.Query Aggregate testing', () => { .catch(done.fail); }); + it('distinct objectId', async () => { + const query = new Parse.Query(TestObject); + const results = await query.distinct('objectId'); + expect(results.length).toBe(4); + }); + + it('distinct createdAt', async () => { + const object1 = new TestObject({ createdAt_test: true }); + await object1.save(); + const object2 = new TestObject({ createdAt_test: true }); + await object2.save(); + const query = new Parse.Query(TestObject); + query.equalTo('createdAt_test', true); + const results = await query.distinct('createdAt'); + expect(results.length).toBe(2); + }); + + it('distinct updatedAt', async () => { + const object1 = new TestObject({ updatedAt_test: true }); + await object1.save(); + const object2 = new TestObject(); + await object2.save(); + object2.set('updatedAt_test', true); + await object2.save(); + const query = new Parse.Query(TestObject); + query.equalTo('updatedAt_test', true); + const results = await query.distinct('updatedAt'); + expect(results.length).toBe(2); + }); + it('distinct null field', done => { const options = Object.assign({}, masterKeyOptions, { body: { distinct: 'distinctField' }, diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index bbf6bb95b8..b0e3351ee9 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -710,19 +710,20 @@ export class MongoStorageAdapter implements StorageAdapter { schema = convertParseSchemaToMongoSchema(schema); const isPointerField = schema.fields[fieldName] && schema.fields[fieldName].type === 'Pointer'; - if (isPointerField) { - fieldName = `_p_${fieldName}`; - } + const transformField = transformKey(className, fieldName, schema); + return this._adaptiveCollection(className) .then(collection => - collection.distinct(fieldName, transformWhere(className, query, schema)) + collection.distinct( + transformField, + transformWhere(className, query, schema) + ) ) .then(objects => { objects = objects.filter(obj => obj != null); return objects.map(object => { if (isPointerField) { - const field = fieldName.substring(3); - return transformPointerString(schema, field, object); + return transformPointerString(schema, fieldName, object); } return mongoObjectToParseObject(className, object, schema); });