devitek / monga
MongoDB 抽象层
1.3.1
2017-09-20 09:01 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: 2.3.*
Replaces
- php-loep/monga: 1.3.1
README
为 PHP 5.4+ 提供简单快捷的 MongoDB 抽象层
这是关于什么的?
- 一个简单的 API,用于获取连接、数据库和集合。
- 一个不会让你头脑混乱的过滤器构建器。
- 各种方便的更新函数。
- 对单个结果排序的抽象。
- 支持 GridFS 的 Mongo 文件系统。
- 简单的聚合和去重。
愿景
Monga 的创建是在认识到 MongoDB PHP 包已经非常出色的情况下进行的。因此,在许多情况下,Monga 只是 MongoDB 类的一个简单包装。它提供了一些辅助工具,并帮助你使用查询构建器设置查询。你也可以选择不使用它!一切仍然会按预期工作。在开发过程中,我们投入了大量精力创建了一个优雅、流畅的 API,该 API 严格遵循 MongoDB 基础类,同时补充了现有类似 SQL 数据库的查询构建器。
安装
通过 Composer
$ composer require league/monga
用法
use League\Monga; // Get a connection $connection = Monga::connection($dns, $connectionOptions); // Get the database $database = $connection->database('db_name'); // Drop the database $database->drop(); // Get a collection $collection = $database->collection('collection_name'); // Drop the collection $collection->drop(); // Truncate the collection $collection->truncate(); // Insert some values into the collection $insertIds = $collection->insert([ [ 'name' => 'John', 'surname' => 'Doe', 'nick' => 'The Unknown Man', 'age' => 20, ], [ 'name' => 'Frank', 'surname' => 'de Jonge', 'nick' => 'Unknown', 'nik' => 'No Man', 'age' => 23, ], ]); // Update a collection $collection->update(function ($query) { $query->increment('age') ->remove('nik') ->set('nick', 'FrenkyNet'); }); // Find Frank $frank = $collection->findOne(function ($query) { $query->where('name', 'Frank') ->whereLike('surname', '%e Jo%'); }); // Or find him using normal array syntax $frank = $collection->find([ 'name' => 'Frank', 'surname' => new MongoRegex('/e Jo/imxsu') ]); $frank['age']++; $collection->save($frank); // Also supports nested queries $users = $collection->find(function ($query) { $query->where(function ($query) { $query->where('name', 'Josh') ->orWhere('surname', 'Doe'); })->orWhere(function ($query) { $query->where('name', 'Frank') ->where('surname', 'de Jonge'); }); }); // get the users as an array $arr = $users->toArray();
聚合
新发布的 MongoDB pecl 包的大部分内容是聚合支持。使用 Monga 来做这一点非常简单。
$collection->aggregate(function ($a) { $a->project([ 'name' => 1, 'surname' => -1, 'tags' => 1, ])->unwind('tags'); // But also more advanced groups/projections $a->project(function ($p) { $p->select('field') ->select('scores') ->exclude('other_field'); })->group(function ($g) { $g->by(['$name', '$surname']) ->sum('scores'); }); });
贡献
请参阅 CONTRIBUTING 和 CONDUCT 了解详细信息。
安全
如果你发现任何与安全相关的问题,请通过电子邮件 bryan@bryan-crowe.com 而不是使用问题跟踪器。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。