league/monga

MongoDB抽象层

1.2.4 2016-02-29 17:56 UTC

This package is auto-updated.

Last update: 2024-09-06 09:01:34 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

一个简单的、快速为PHP 5.4+提供的MongoDB抽象层

这一切都是关于什么的?

  • 一个易于使用的API,用于获取连接、数据库和集合。
  • 一个不会让你头昏脑胀的过滤器构建器。
  • 各种实用的更新函数。
  • 对单个结果的排序抽象。
  • 对Mongo文件系统的GridFS支持。
  • 轻松聚合和去重。

愿景

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

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

安全

如果你发现任何安全相关的问题,请通过电子邮件bryan@bryan-crowe.com联系,而不是使用问题跟踪器。

致谢

许可协议

MIT许可协议(MIT)。请参阅许可文件以获取更多信息。