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