Core PHPLIB class instances needed for CRUD operations

It is important to note that all of the CRUD operations that we are going to discuss require that you create a MongoDB\Collection instance. This can be accomplished in several ways, including:

  • MongoDB\Client::selectCollection()
  • MongoDB\Database::selectCollection()
  • MongoDB\Collection::__construct()

Of the three choices, we will focus on the first, since the MongoDB\Client consumes the database and thus provides more flexibility. To create a MongoDB\Client instance, proceed as follows:

  1. First, define a connection string URI following this format:
      mongodb://[username:password@]host[:port][/[database][?options]]
  1. Create a MongoDB\Client instance using the URI as an argument:
 $client ...

Get MongoDB 4 Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.