php-loep / monga
1.2.4
2016-02-29 17:56 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.2.4
README
一个简单快速且适用于PHP 5.4+的MongoDB抽象层
这是什么?
- 一个简单的API来获取连接、数据库和集合。
- 一个不会让你头疼的过滤器构建器。
- 各种方便的更新函数。
- 对单个结果的排序抽象。
- 对Mongo文件系统的GridFS支持。
- 简单的聚合和distinct值。
愿景
Monga的创建是基于MongoDB PHP包已经很出色的认识。这就是为什么在许多情况下,Monga只是MongoDB类的简单包装。它提供了一些辅助工具,并帮助您使用查询构建器设置查询。您也可以选择不使用它!一切都会相应地工作。在开发过程中,我们投入了大量计划,创建了一个流畅的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)。请参阅许可证文件以获取更多信息。