From 1223a86cb1ba275e1724c85becdc797e7e1ae43d Mon Sep 17 00:00:00 2001 From: Corey's iMac Date: Sun, 20 Jun 2021 19:41:58 -0400 Subject: [PATCH 1/3] Fix: explain should return as an array of results --- integration/test/ParseQueryTest.js | 4 +++- src/ParseQuery.js | 4 ++++ src/__tests__/ParseQuery-test.js | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/integration/test/ParseQueryTest.js b/integration/test/ParseQueryTest.js index 90f82922e..d24530ec6 100644 --- a/integration/test/ParseQueryTest.js +++ b/integration/test/ParseQueryTest.js @@ -2355,7 +2355,9 @@ describe('Parse Query', () => { query.hint('_id_'); query.explain(); const explain = await query.find(); - assert.equal(explain.queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); + assert.equal(explain[0].queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); + const explainFirst = await query.first(); + assert.equal(explainFirst.queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); }); it('can query with select on null field', async () => { diff --git a/src/ParseQuery.js b/src/ParseQuery.js index fe5c6f0ea..ae4483da8 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -899,6 +899,10 @@ class ParseQuery { if (!objects[0]) { return undefined; } + // Return generic object when explain is used + if (this._explain) { + return objects[0]; + } if (!objects[0].className) { objects[0].className = this.className; } diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js index 89150dc03..99e94abc1 100644 --- a/src/__tests__/ParseQuery-test.js +++ b/src/__tests__/ParseQuery-test.js @@ -1302,6 +1302,14 @@ describe('ParseQuery', () => { q.explain(); q.equalTo('size', 'small') .find() + .then(results => { + expect(results[0].objectId).toBe('I1'); + expect(results[0].size).toBe('small'); + expect(results[0].name).toEqual('Product 3'); + done(); + }); + q.equalTo('size', 'small') + .first() .then(result => { expect(result.objectId).toBe('I1'); expect(result.size).toBe('small'); From a25a21b70cc2653182429512f3ce83c9d3fa3405 Mon Sep 17 00:00:00 2001 From: Corey Date: Sat, 24 Jun 2023 19:02:57 -0400 Subject: [PATCH 2/3] Update ParseQueryTest.js --- integration/test/ParseQueryTest.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/integration/test/ParseQueryTest.js b/integration/test/ParseQueryTest.js index 0a83bf582..996e05bc9 100644 --- a/integration/test/ParseQueryTest.js +++ b/integration/test/ParseQueryTest.js @@ -2359,18 +2359,19 @@ describe('Parse Query', () => { query.hint('_id_'); query.explain(); const explain = await query.find(); - assert.equal(explain[0].queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); - const explainFirst = await query.first(); - assert.equal(explainFirst.queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); - let indexName = ''; // https://www.mongodb.com/docs/manual/reference/explain-results/#std-label-queryPlanner - const plan = explain.queryPlanner.winningPlan; + const plan = explain[0].queryPlanner.winningPlan; + let indexName = ''; + if (plan.inputStage) { indexName = plan.inputStage.inputStage.indexName; } else { indexName = plan.queryPlan.inputStage.inputStage.indexName; } assert.equal(indexName, '_id_'); + + const explainFirst = await query.first(); + assert.equal(explainFirst.queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); }); it('can query with select on null field', async () => { From b4539ce0dfe62f4bbde2db654e7aab84d9c26e78 Mon Sep 17 00:00:00 2001 From: Corey Date: Sat, 24 Jun 2023 19:05:51 -0400 Subject: [PATCH 3/3] Update ParseQueryTest.js --- integration/test/ParseQueryTest.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/integration/test/ParseQueryTest.js b/integration/test/ParseQueryTest.js index 996e05bc9..4dfd758f8 100644 --- a/integration/test/ParseQueryTest.js +++ b/integration/test/ParseQueryTest.js @@ -2359,17 +2359,16 @@ describe('Parse Query', () => { query.hint('_id_'); query.explain(); const explain = await query.find(); + let indexName = ''; // https://www.mongodb.com/docs/manual/reference/explain-results/#std-label-queryPlanner const plan = explain[0].queryPlanner.winningPlan; - let indexName = ''; - if (plan.inputStage) { indexName = plan.inputStage.inputStage.indexName; } else { indexName = plan.queryPlan.inputStage.inputStage.indexName; } assert.equal(indexName, '_id_'); - + const explainFirst = await query.first(); assert.equal(explainFirst.queryPlanner.winningPlan.inputStage.inputStage.indexName, '_id_'); });