title: In MongoDB, insertOne is not returning the document object if executed via REST API. What is wrong?
body: <p>I am using mongoDB with PHP. I would like to figure out why the insertOne function does not return the</p>
  
  <pre><code>$insertedDocument = $collection-&gt;insertOne($data)
  </code></pre>
  
  <p>even after successfully adding it to the collection on the server.</p>
  
  <p>But, if used on a regular PHP page, the function does return the $insertedDocument (verified using var_dump).</p>
  
  <p>Case 1:
  1. A simple page with a PHP block that uses the
      $insertedDocument = $collection->insertOne($data).
  2. Outputs the $insertedDocument correctly, and as expected.</p>
  
  <p>Case 2:
  1. Using Axios, when sending a POST request to <a href="https://domain.tld/namespace/endpoint/" rel="nofollow noreferrer">https://domain.tld/namespace/endpoint/</a>, $data is appended to the $body of the request.
  2. $data is recieved as expected
  3. When using the same function (copied and pasted it in a PHP block on a static page as is for debugging), $insertedDocument is null.
  4. But, the data is successfully store on the server. Verified the data of the $insertedDocument in the $collection on the server.</p>
  
  <pre><code>try {
    $insertedDocument = $this-&gt;mdb_collection-&gt;insertOne(array(
      'key1' =&gt; $data1,
      'key2' =&gt; $data2,
      'key3' =&gt; $data3,
      'key4' =&gt; new UTCDateTime
    ));
  } catch (Exception $e) {
    return new WP_Error('error-registering', __('There was an error while registering', 'domain'));
  }
  
  return $insertedDocument;
  </code></pre>
  
  <p>With the REST API, insertOne does not return anything, when the actual expected return value is the $insertedDocument.</p>