From dca4125b2b5e6a7416f009384956125ca84030e3 Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Sat, 15 Feb 2020 01:08:40 +0330 Subject: [PATCH 1/2] Update Query.php --- src/Query.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Query.php b/src/Query.php index f1a8c2386..5dc0c1acc 100644 --- a/src/Query.php +++ b/src/Query.php @@ -495,7 +495,24 @@ public function exists($db = null) if (!empty($this->emulateExecution)) { return false; } + + #better performance + #save last options + $tmpOrderBy = $this->orderBy; + $tmpLimit = $this->limit; + $tmpOffset = $this->offset; + $tmpSelect = $this->select; + $this->orderBy = []; + $this->limit = 1; + $this->offset = null; + $this->select = ['_id']; $cursor = $this->buildCursor($db); + #return last options + $this->orderBy = $tmpOrderBy; + $this->limit = $tmpLimit; + $this->offset = $tmpOffset; + $this->select = $tmpSelect; + foreach ($cursor as $row) { return true; } From 633ed7385011b9c6c448ee91ae2eddef21ca467d Mon Sep 17 00:00:00 2001 From: Ziaratban Date: Tue, 25 Feb 2020 10:42:10 +0330 Subject: [PATCH 2/2] Update Query.php --- src/Query.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 5dc0c1acc..95c686393 100644 --- a/src/Query.php +++ b/src/Query.php @@ -496,7 +496,11 @@ public function exists($db = null) return false; } - #better performance + /** + * better performance + * please read this article : Checking if a document exists – MongoDB slow findOne vs find + * https://blog.serverdensity.com/checking-if-a-document-exists-mongodb-slow-findone-vs-find/ + */ #save last options $tmpOrderBy = $this->orderBy; $tmpLimit = $this->limit;