From 61678c9b6259b91aec39624bcad27358128cef0c Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 14:55:34 -0400
Subject: [PATCH 01/14] DOCSP-43518: query logging

---
 docs/fundamentals/read-operations.txt         |   3 +-
 .../read-operations/query-logging.txt         | 115 ++++++++++++++++++
 .../read-operations/ReadOperationsTest.php    |  22 ++++
 3 files changed, 139 insertions(+), 1 deletion(-)
 create mode 100644 docs/fundamentals/read-operations/query-logging.txt

diff --git a/docs/fundamentals/read-operations.txt b/docs/fundamentals/read-operations.txt
index 367e2d38d..674615ffb 100644
--- a/docs/fundamentals/read-operations.txt
+++ b/docs/fundamentals/read-operations.txt
@@ -17,7 +17,8 @@ Read Operations
    Retrieve Data </fundamentals/read-operations/retrieve>
    Search Text </fundamentals/read-operations/search-text>
    Modify Query Results </fundamentals/read-operations/modify-results>
-   Set Read Preference </fundamentals/read-operations/read-pref>
+   Read Preference </fundamentals/read-operations/read-pref>
+   Query Logging </fundamentals/read-operations/query-logging>
 
 .. contents:: On this page
    :local:
diff --git a/docs/fundamentals/read-operations/query-logging.txt b/docs/fundamentals/read-operations/query-logging.txt
new file mode 100644
index 000000000..4a44d1251
--- /dev/null
+++ b/docs/fundamentals/read-operations/query-logging.txt
@@ -0,0 +1,115 @@
+.. _laravel-query-logging:
+
+====================
+Enable Query Logging
+====================
+
+.. facet::
+   :name: genre
+   :values: reference
+
+.. meta::
+   :keywords: monitoring, CRUD, code example
+
+.. contents:: On this page
+   :local:
+   :backlinks: none
+   :depth: 2
+   :class: singlecol
+
+Overview
+--------
+
+In this guide, you can learn how to enable query logging in
+{+odm-long+}. Query logging can help you debug your queries and monitor
+database interactions.
+
+.. include:: /includes/fundamentals/read-operations/before-you-get-started.rst
+
+Enable Logs On a Connection
+---------------------------
+
+To enable logs on a connection, you can use the ``enableQueryLog()``
+method on the ``DB`` facade. This method enables MongoDB command logging
+on any queries that you perform on the database connection.
+
+After you enable query logging, any queries you perform are stored in
+memory. To retrieve the logs, use one of the following methods:
+
+- ``getQueryLog()``: Returns a log of MongoDB queries
+- ``getRawQueryLog()``: Returns a log of raw MongoDB queries
+
+The following example enables query logging, performs some queries, then
+prints the query log:
+
+.. tabs::
+
+   .. tab:: Query Syntax
+      :tabid: query-syntax
+
+      Use the following syntax to specify the query:
+
+      .. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
+         :language: php
+         :dedent:
+         :start-after: start-query-log
+         :end-before: end-query-log
+         :emphasize-lines: 1, 7
+
+   .. tab:: Controller Method
+      :tabid: controller
+
+      To see the logs in the ``browse_movies`` view, edit the ``show()`` function
+      in the ``MovieController.php`` file to resemble the following code:
+
+      .. io-code-block::
+         :copyable: true
+
+         .. input::
+            :language: php
+
+            class MovieController
+            {
+                public function show()
+                {
+                    DB::connection('mongodb')->enableQueryLog();
+            
+                    Movie::where('title', 'Carrie')->get();
+                    Movie::where('year', '<', 2005)->get();
+                    Movie::where('imdb.rating', '>', 8.5)->get();
+            
+                    $logs = DB::connection('mongodb')->getQueryLog();
+                    foreach ($logs as $log) {
+                        echo json_encode($log, JSON_PRETTY_PRINT);
+                    }
+                }
+            }
+
+         .. output::
+            :language: none
+            :visible: false
+            
+            {
+              "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }",
+              "bindings": [],
+              "time": 29476
+            }
+            {
+              "query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }",
+              "bindings": [],
+              "time": 29861
+            }
+            {
+              "query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }",
+              "bindings": [],
+              "time": 27251
+            }
+
+Additional Information
+----------------------
+
+To learn more about connecting to MongoDB, see the
+:ref:`laravel-connect-to-mongodb`.
+
+To learn how to retrieve data based on filter criteria, see the
+:ref:`laravel-fundamentals-read-retrieve` guide.
diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 207fd442e..c87223ca8 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -183,4 +183,26 @@ public function testReadPreference(): void
         $this->assertNotNull($movies);
         $this->assertCount(2, $movies);
     }
+
+    /**
+     * @runInSeparateProcess
+     * @preserveGlobalState disabled
+     */
+    public function testQueryLog(): void
+    {
+        // start-query-log
+        DB::connection('mongodb')->enableQueryLog();
+
+        Movie::where('title', 'Carrie')->get();
+        Movie::where('year', '<', 2005)->get();
+        Movie::where('imdb.rating', '>', 8.5)->get();
+
+        $logs = DB::connection('mongodb')->getQueryLog();
+        foreach ($logs as $log) {
+            echo json_encode($log, JSON_PRETTY_PRINT);
+        }
+        // end-query-log
+
+        $this->assertNotNull($logs);
+    }
 }

From 5e8ddf64dfc1bd7a3ca34866b4bc5a33b75fef0a Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 14:57:58 -0400
Subject: [PATCH 02/14] formatting

---
 .../fundamentals/read-operations/ReadOperationsTest.php      | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index c87223ca8..7ab61327a 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -9,6 +9,10 @@
 use MongoDB\Driver\ReadPreference;
 use MongoDB\Laravel\Tests\TestCase;
 
+use function json_encode;
+
+use const JSON_PRETTY_PRINT;
+
 class ReadOperationsTest extends TestCase
 {
     protected function setUp(): void
@@ -201,6 +205,7 @@ public function testQueryLog(): void
         foreach ($logs as $log) {
             echo json_encode($log, JSON_PRETTY_PRINT);
         }
+
         // end-query-log
 
         $this->assertNotNull($logs);

From 9f64a9be029fe74d38e0539e97531a8ecaac023e Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 15:03:11 -0400
Subject: [PATCH 03/14] remove controller

---
 .../read-operations/query-logging.txt         | 89 ++++++-------------
 1 file changed, 28 insertions(+), 61 deletions(-)

diff --git a/docs/fundamentals/read-operations/query-logging.txt b/docs/fundamentals/read-operations/query-logging.txt
index 4a44d1251..19c61b0ea 100644
--- a/docs/fundamentals/read-operations/query-logging.txt
+++ b/docs/fundamentals/read-operations/query-logging.txt
@@ -42,68 +42,35 @@ memory. To retrieve the logs, use one of the following methods:
 The following example enables query logging, performs some queries, then
 prints the query log:
 
-.. tabs::
-
-   .. tab:: Query Syntax
-      :tabid: query-syntax
-
-      Use the following syntax to specify the query:
-
-      .. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
-         :language: php
-         :dedent:
-         :start-after: start-query-log
-         :end-before: end-query-log
-         :emphasize-lines: 1, 7
-
-   .. tab:: Controller Method
-      :tabid: controller
-
-      To see the logs in the ``browse_movies`` view, edit the ``show()`` function
-      in the ``MovieController.php`` file to resemble the following code:
-
-      .. io-code-block::
-         :copyable: true
-
-         .. input::
-            :language: php
-
-            class MovieController
-            {
-                public function show()
-                {
-                    DB::connection('mongodb')->enableQueryLog();
-            
-                    Movie::where('title', 'Carrie')->get();
-                    Movie::where('year', '<', 2005)->get();
-                    Movie::where('imdb.rating', '>', 8.5)->get();
-            
-                    $logs = DB::connection('mongodb')->getQueryLog();
-                    foreach ($logs as $log) {
-                        echo json_encode($log, JSON_PRETTY_PRINT);
-                    }
-                }
-            }
-
-         .. output::
-            :language: none
-            :visible: false
+.. io-code-block::
+   :copyable: true
+
+   .. input:: /includes/fundamentals/read-operations/ReadOperationsTest.php
+      :language: php
+      :dedent:
+      :start-after: start-query-log
+      :end-before: end-query-log
+      :emphasize-lines: 1, 7
+
+   .. output::
+      :language: json
+      :visible: false
             
-            {
-              "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }",
-              "bindings": [],
-              "time": 29476
-            }
-            {
-              "query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }",
-              "bindings": [],
-              "time": 29861
-            }
-            {
-              "query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }",
-              "bindings": [],
-              "time": 27251
-            }
+      {
+        "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }",
+        "bindings": [],
+        "time": 29476
+      }
+      {
+        "query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }",
+        "bindings": [],
+        "time": 29861
+      }
+      {
+        "query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }",
+        "bindings": [],
+        "time": 27251
+      }
 
 Additional Information
 ----------------------

From 6db996135d6bc557f719f83886e74b4423fbc64d Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 15:10:25 -0400
Subject: [PATCH 04/14] tests

---
 .../fundamentals/read-operations/ReadOperationsTest.php         | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 7ab61327a..7a5c35269 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -208,6 +208,8 @@ public function testQueryLog(): void
 
         // end-query-log
 
+        $this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","id":"[a-z0-9]{24}"}$/');
+        $this->expectOutputString('{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }", "bindings": [], "time": 35366 }{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }", "bindings": [], "time": 31571 }{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }", "bindings": [], "time": 26469 }');
         $this->assertNotNull($logs);
     }
 }

From b3ca463edc7faca2d560f2495f774689b94aac2d Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 15:12:48 -0400
Subject: [PATCH 05/14] tests

---
 .../includes/fundamentals/read-operations/ReadOperationsTest.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 7a5c35269..2006d38bc 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -208,7 +208,6 @@ public function testQueryLog(): void
 
         // end-query-log
 
-        $this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","id":"[a-z0-9]{24}"}$/');
         $this->expectOutputString('{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }", "bindings": [], "time": 35366 }{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }", "bindings": [], "time": 31571 }{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }", "bindings": [], "time": 26469 }');
         $this->assertNotNull($logs);
     }

From c53d32a53a64b03734b266679d9854cfa95b3cb9 Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 15:21:25 -0400
Subject: [PATCH 06/14] tests

---
 .../fundamentals/read-operations/ReadOperationsTest.php         | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 2006d38bc..448aa3262 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -208,7 +208,7 @@ public function testQueryLog(): void
 
         // end-query-log
 
-        $this->expectOutputString('{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }", "bindings": [], "time": 35366 }{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"year\" : { \"$lt\" : { \"$numberInt\" : \"2005\" } } } }", "bindings": [], "time": 31571 }{ "query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }", "bindings": [], "time": 26469 }');
+        $this->expectOutputRegex('\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"title\\" : \\"Carrie\\" \} \}", "bindings": \[\], "time": [0-9]+ \}\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"year\\" : \{ \\"\$lt\\" : \{ \\"\$numberInt\\" : \\"2005\\" \} \} \} \}", "bindings": \[\], "time": [0-9]+ \}\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"imdb\.rating\\" : \{ \\"\$gt\\" : \{ \\"\$numberDouble\\" : \\"8\.5\\" \} \} \} \}", "bindings": \[\], "time": [0-9]+ \}');
         $this->assertNotNull($logs);
     }
 }

From 1ced04a81e721b3932d8fe2099bb9e250ede973d Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Mon, 10 Mar 2025 15:26:22 -0400
Subject: [PATCH 07/14] tests

---
 .../includes/fundamentals/read-operations/ReadOperationsTest.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 448aa3262..6a6e27715 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -209,6 +209,5 @@ public function testQueryLog(): void
         // end-query-log
 
         $this->expectOutputRegex('\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"title\\" : \\"Carrie\\" \} \}", "bindings": \[\], "time": [0-9]+ \}\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"year\\" : \{ \\"\$lt\\" : \{ \\"\$numberInt\\" : \\"2005\\" \} \} \} \}", "bindings": \[\], "time": [0-9]+ \}\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"imdb\.rating\\" : \{ \\"\$gt\\" : \{ \\"\$numberDouble\\" : \\"8\.5\\" \} \} \} \}", "bindings": \[\], "time": [0-9]+ \}');
-        $this->assertNotNull($logs);
     }
 }

From fe23928ec5fcd9101c72b57e6c52caddae08110c Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Fri, 21 Mar 2025 13:53:06 -0400
Subject: [PATCH 08/14] test

---
 .../fundamentals/read-operations/ReadOperationsTest.php     | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 6a6e27715..9ac74d8b5 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -11,8 +11,6 @@
 
 use function json_encode;
 
-use const JSON_PRETTY_PRINT;
-
 class ReadOperationsTest extends TestCase
 {
     protected function setUp(): void
@@ -203,11 +201,11 @@ public function testQueryLog(): void
 
         $logs = DB::connection('mongodb')->getQueryLog();
         foreach ($logs as $log) {
-            echo json_encode($log, JSON_PRETTY_PRINT);
+            echo json_encode($log);
         }
 
         // end-query-log
 
-        $this->expectOutputRegex('\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"title\\" : \\"Carrie\\" \} \}", "bindings": \[\], "time": [0-9]+ \}\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"year\\" : \{ \\"\$lt\\" : \{ \\"\$numberInt\\" : \\"2005\\" \} \} \} \}", "bindings": \[\], "time": [0-9]+ \}\{ "query": "\{ \\"find\\" : \\"movies\\", \\"filter\\" : \{ \\"imdb\.rating\\" : \{ \\"\$gt\\" : \{ \\"\$numberDouble\\" : \\"8\.5\\" \} \} \} \}", "bindings": \[\], "time": [0-9]+ \}');
+        $this->expectOutputRegex('');
     }
 }

From a3e13c8e5719f9c393f719673e2871971048e208 Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Fri, 21 Mar 2025 13:57:03 -0400
Subject: [PATCH 09/14] test

---
 docs/fundamentals/read-operations/query-logging.txt       | 2 +-
 .../fundamentals/read-operations/ReadOperationsTest.php   | 8 +-------
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/docs/fundamentals/read-operations/query-logging.txt b/docs/fundamentals/read-operations/query-logging.txt
index 19c61b0ea..27816b298 100644
--- a/docs/fundamentals/read-operations/query-logging.txt
+++ b/docs/fundamentals/read-operations/query-logging.txt
@@ -55,7 +55,7 @@ prints the query log:
    .. output::
       :language: json
       :visible: false
-            
+
       {
         "query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }",
         "bindings": [],
diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 9ac74d8b5..41aecf40e 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -9,8 +9,6 @@
 use MongoDB\Driver\ReadPreference;
 use MongoDB\Laravel\Tests\TestCase;
 
-use function json_encode;
-
 class ReadOperationsTest extends TestCase
 {
     protected function setUp(): void
@@ -200,12 +198,8 @@ public function testQueryLog(): void
         Movie::where('imdb.rating', '>', 8.5)->get();
 
         $logs = DB::connection('mongodb')->getQueryLog();
-        foreach ($logs as $log) {
-            echo json_encode($log);
-        }
-
         // end-query-log
 
-        $this->expectOutputRegex('');
+        $this->assertNotNull($logs);
     }
 }

From c24981d64729d28ce896b51878ea2c886fc6f30d Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Fri, 21 Mar 2025 14:00:21 -0400
Subject: [PATCH 10/14] test

---
 .../read-operations/ReadOperationsTest.php             | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 41aecf40e..1cbf51039 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -9,6 +9,11 @@
 use MongoDB\Driver\ReadPreference;
 use MongoDB\Laravel\Tests\TestCase;
 
+use function json_encode;
+use function ob_flush;
+
+use const JSON_PRETTY_PRINT;
+
 class ReadOperationsTest extends TestCase
 {
     protected function setUp(): void
@@ -198,8 +203,13 @@ public function testQueryLog(): void
         Movie::where('imdb.rating', '>', 8.5)->get();
 
         $logs = DB::connection('mongodb')->getQueryLog();
+        foreach ($logs as $log) {
+            echo json_encode($log, JSON_PRETTY_PRINT);
+        }
         // end-query-log
 
+        ob_flush();
+        $logs = DB::connection('mongodb')->getQueryLog();
         $this->assertNotNull($logs);
     }
 }

From d89eac59fb2b791c896b1393c83f9ce1c650b733 Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Fri, 21 Mar 2025 14:04:08 -0400
Subject: [PATCH 11/14] test

---
 .../includes/fundamentals/read-operations/ReadOperationsTest.php | 1 +
 1 file changed, 1 insertion(+)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 1cbf51039..3ff29d763 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -206,6 +206,7 @@ public function testQueryLog(): void
         foreach ($logs as $log) {
             echo json_encode($log, JSON_PRETTY_PRINT);
         }
+
         // end-query-log
 
         ob_flush();

From 91f7e77375f20afffc218ea2e2ebb8acc316eae6 Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Fri, 21 Mar 2025 14:21:33 -0400
Subject: [PATCH 12/14] test

---
 .../read-operations/ReadOperationsTest.php    | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 3ff29d763..9b81061ed 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -209,8 +209,23 @@ public function testQueryLog(): void
 
         // end-query-log
 
-        ob_flush();
-        $logs = DB::connection('mongodb')->getQueryLog();
         $this->assertNotNull($logs);
+        $this->expectOutputRegex('/^'
+            . '\{'
+            . '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"title\\\"\s*:\s*\\\"Carrie\\\"\}\}",'
+            . '\s*"bindings"\s*:\s*\[\],'
+            . '\s*"time"\s*:\s*\d+'
+            . '\}'
+            . '\{'
+            . '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"year\\\"\s*:\s*\{\\\"\\$lt\\\"\s*:\s*\{\\\"\\$numberInt\\\"\s*:\s*\\\"2005\\\"\}\}\}\}",'
+            . '\s*"bindings"\s*:\s*\[\],'
+            . '\s*"time"\s*:\s*\d+'
+            . '\}'
+            . '\{'
+            . '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"imdb.rating\\\"\s*:\s*\{\\\"\\$gt\\\"\s*:\s*\{\\\"\\$numberDouble\\\"\s*:\s*\\\"8\.5\\\"\}\}\}\}",'
+            . '\s*"bindings"\s*:\s*\[\],'
+            . '\s*"time"\s*:\s*\d+'
+            . '\}'
+            . '$/x');
     }
 }

From ca507616223b12d7241166c54c443d24991c8e86 Mon Sep 17 00:00:00 2001
From: rustagir <rea.rustagi@mongodb.com>
Date: Fri, 21 Mar 2025 14:21:46 -0400
Subject: [PATCH 13/14] formatting

---
 .../includes/fundamentals/read-operations/ReadOperationsTest.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index 9b81061ed..ebac8e7fe 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -10,7 +10,6 @@
 use MongoDB\Laravel\Tests\TestCase;
 
 use function json_encode;
-use function ob_flush;
 
 use const JSON_PRETTY_PRINT;
 

From d589effa1ca9551b0ec6430a5f788da0ad531b28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?=
 <jerome.tamarelle@mongodb.com>
Date: Fri, 9 May 2025 12:23:07 +0200
Subject: [PATCH 14/14] Fix test on output

---
 .../read-operations/ReadOperationsTest.php    | 34 ++++++++-----------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
index ebac8e7fe..414b21d31 100644
--- a/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
+++ b/docs/includes/fundamentals/read-operations/ReadOperationsTest.php
@@ -10,8 +10,11 @@
 use MongoDB\Laravel\Tests\TestCase;
 
 use function json_encode;
+use function ob_get_flush;
+use function ob_start;
 
 use const JSON_PRETTY_PRINT;
+use const PHP_EOL;
 
 class ReadOperationsTest extends TestCase
 {
@@ -194,6 +197,10 @@ public function testReadPreference(): void
      */
     public function testQueryLog(): void
     {
+        $output = '';
+        ob_start(function (string $buffer) use (&$output) {
+            $output .= $buffer;
+        });
         // start-query-log
         DB::connection('mongodb')->enableQueryLog();
 
@@ -203,28 +210,17 @@ public function testQueryLog(): void
 
         $logs = DB::connection('mongodb')->getQueryLog();
         foreach ($logs as $log) {
-            echo json_encode($log, JSON_PRETTY_PRINT);
+            echo json_encode($log, JSON_PRETTY_PRINT) . PHP_EOL;
         }
 
         // end-query-log
-
+        $output = ob_get_flush();
         $this->assertNotNull($logs);
-        $this->expectOutputRegex('/^'
-            . '\{'
-            . '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"title\\\"\s*:\s*\\\"Carrie\\\"\}\}",'
-            . '\s*"bindings"\s*:\s*\[\],'
-            . '\s*"time"\s*:\s*\d+'
-            . '\}'
-            . '\{'
-            . '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"year\\\"\s*:\s*\{\\\"\\$lt\\\"\s*:\s*\{\\\"\\$numberInt\\\"\s*:\s*\\\"2005\\\"\}\}\}\}",'
-            . '\s*"bindings"\s*:\s*\[\],'
-            . '\s*"time"\s*:\s*\d+'
-            . '\}'
-            . '\{'
-            . '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"imdb.rating\\\"\s*:\s*\{\\\"\\$gt\\\"\s*:\s*\{\\\"\\$numberDouble\\\"\s*:\s*\\\"8\.5\\\"\}\}\}\}",'
-            . '\s*"bindings"\s*:\s*\[\],'
-            . '\s*"time"\s*:\s*\d+'
-            . '\}'
-            . '$/x');
+        $this->assertNotEmpty($output);
+
+        $this->assertStringContainsString('"query": "{ \"find\" : \"movies\", \"filter\" : { \"title\" : \"Carrie\" } }"', $output);
+        $this->assertStringContainsString('"query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }"', $output);
+        $this->assertStringContainsString('"query": "{ \"find\" : \"movies\", \"filter\" : { \"imdb.rating\" : { \"$gt\" : { \"$numberDouble\" : \"8.5\" } } } }"', $output);
+        $this->assertMatchesRegularExpression('/"time": \d+/', $output);
     }
 }