utopia-php/mongo

管理Mongo数据库的简单库

0.3.1 2023-09-01 17:25 UTC

README

Build Status Total Downloads Discord

一个基于MongoDB行协议的非阻塞PHP客户端。设计用于在异步PHP环境(如 Swoole)中良好工作。这个库旨在简单易学易用。该库由 Appwrite 团队 维护。

入门指南

使用composer安装

composer require utopia-php/mongo

在您的应用程序中初始化

<?php

$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');

系统需求

Utopia Mongo客户端需要PHP 8.0或更高版本。我们建议尽可能使用最新版本的PHP。

版权和许可证

MIT许可证(MIT) http://www.opensource.org/licenses/mit-license.php