1. Go to this page and download the library: Download utopia-php/mongo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
utopia-php / mongo example snippets
$client = new Client('testing', 'mongo', 27017, 'root', 'example', false);
$client->connect();
// drop database
$client->dropDatabase([]);
// Create a new collection
$client->createCollection('movies');
// Get the list of databases
$client->listDatabaseNames()->databases;
// insert a new document
$document = $client->insert('movies', [
'name' => 'Armageddon 1',
'country' => 'USA',
'language' => 'English'
]
);
$id = (string)$document['_id'];
// Find Document with ObjectId
$client->find('movies', ['_id' => new ObjectId($id)])->cursor->firstBatch ?? [];
// insert a new document with specific id
$id = 999;
$client->insert('movies', [
'name' => 'Armageddon 2',
'_id' => $id,
'array' => ['USA', 'UK', 'India'],
'language' => 'English',
'float' => 9.9,
'integer' => 9,
'is_open' => true,
'date_string' => (new \DateTime())->format('Y-m-d\TH:i:s.vP'),
]
);
// Find document by id
$client->find('movies', ['_id' => $id])->cursor->firstBatch ?? [];
// Find documents by field
$client->find('movies', ['name' => 'Armageddon'])->cursor->firstBatch ?? [];
// Delete a document
$client->delete('movies', ['_id' => $id]);
// drop a collections
$client->dropCollection('movies');
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.