From e4241374061caef66538de15112fb6bbafb1f5bb Mon Sep 17 00:00:00 2001 From: dblythy Date: Sun, 18 Sep 2022 00:19:28 +1000 Subject: [PATCH] fix: query aggregation pipeline cannot handle value of type `Date` when `directAccess: true` (#8167) --- spec/ParseQuery.Aggregate.spec.js | 17 +++++++++++++++++ .../Storage/Mongo/MongoStorageAdapter.js | 3 +++ 2 files changed, 20 insertions(+) diff --git a/spec/ParseQuery.Aggregate.spec.js b/spec/ParseQuery.Aggregate.spec.js index 04fa6068ed..5bfe22683f 100644 --- a/spec/ParseQuery.Aggregate.spec.js +++ b/spec/ParseQuery.Aggregate.spec.js @@ -666,6 +666,23 @@ describe('Parse.Query Aggregate testing', () => { }); }); + it('should aggregate with Date object (directAccess)', async () => { + const rest = require('../lib/rest'); + const auth = require('../lib/Auth'); + const TestObject = Parse.Object.extend('TestObject'); + const date = new Date(); + await new TestObject({ date: date }).save(null, { useMasterKey: true }); + const config = Config.get(Parse.applicationId); + const resp = await rest.find( + config, + auth.master(config), + 'TestObject', + {}, + { pipeline: [{ $match: { date: { $lte: new Date() } } }] } + ); + expect(resp.results.length).toBe(1); + }); + it('match comparison query', done => { const options = Object.assign({}, masterKeyOptions, { body: { diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index 93e23b91c3..c0d4c0ca9e 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -952,6 +952,9 @@ export class MongoStorageAdapter implements StorageAdapter { // an operator in it (like $gt, $lt, etc). Because of this I felt it was easier to make this a // recursive method to traverse down to the "leaf node" which is going to be the string. _convertToDate(value: any): any { + if (value instanceof Date) { + return value; + } if (typeof value === 'string') { return new Date(value); }