monga/monga

该包已被弃用且不再维护。作者建议使用php-loep/monga包。

MongoDB 抽象层

0.2.4 2013-11-22 11:00 UTC

This package is auto-updated.

Last update: 2021-06-15 20:39:47 UTC


README

这是一个简单的、快速的 PHP5.3+ MongoDB 抽象层。

在 Packagist/Composer 中查找 Monga

这是关于什么的?

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

愿景

Monga 的创建是基于 MongoDB PHP 包已经非常出色的认识。这就是为什么在许多情况下,Monga 只是 MongoDB 类的一个简单包装。它提供了一些助手,并帮助你使用查询构建器设置查询。你也可以选择不使用它!所有东西仍然会按相应的方式工作。在开发过程中,投入了大量计划,创建了一个简洁的 API,该 API 与 MongoDB 基础类紧密相关,同时补充了现有的类似 SQL 数据库的查询构建器。

示例

// Get a connection
$connection = Monga::connection($dsn, $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(array(
	array(
		'name' => 'John',
		'surname' => 'Doe',
		'nick' => 'The Unknown Man',
		'age' => 20,
	),
	array(
		'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(array('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->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(array(
		'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(array('$name', '$surname'))
			->sum('scores');
	});
});

如果您需要任何帮助,可以在 IRC 频道(昵称为:FrenkyNet 的 #fuelphp/#cabinet/#mongodb)中找到我。

享受!