Ming (a MongoDB wrapper)
Component ID
1666316
Component name
Ming (a MongoDB wrapper)
Component type
module
Maintenance status
Development status
Component security advisory coverage
not-covered
Downloads
1136
Component created
Component changed
Component body
Drupal 8
This module has been converted into a general PHP library called Vultan, and will not be released as a module for Drupal 8.
Vultan is available on Packagist, and can be added to Drupal via Composer:
https://packagist.org/packages/xtfer/vultan
Drupal 7
Ming is an API for working with data in MongoDB. It was designed to allow rapid construction of MongoDB-based apps in Drupal, where Field integration is not required.
Ming is a wrapper around the PHP Mongo classes, which adds some additional capabilities and behaviours. In addition to what raw Mongo support provides, Ming has the following features:
- Preconfigure and connect to multiple persistent connections, from multiple Mongo database sources, using hooks, site variables, at runtime, or from the Mongodb module
- Simple wrappers around common Mongo tasks (Insert, Upsert, Delete, Find), including some common variants
- Shortcuts for accessing objects by MongoID, without the need to instantiate MongoID classes
- A scaffold class (mingMingItem) for building, saving and working with MongoDB data
Ming does not require the Mongodb module to be installed, but will work with it.
Examples
/**
* Example 1: Connect to a Mongo database
*
* Settings should be provided as a keyed array, documented in hook_ming_settings(),
* however this array can be provided as a simple settings array passed to
* ming_db() as well, or through $conf['ming_settings'].
*
* @return MingCoreDatabase $db
*/
function ming_example_connect() {
// Start with your settings
$settings = array(
'mongo_host' => 'localhost', // default
'mongo_port' => '27017', // default
'mongo_user' => 'my_mongo_user', // defaults to NULL
'mongo_pass' => 'my_mongo_password', // defaults to NULL
'mongo_db' => 'my_db_name', // a default databse for this connection
);
// Load our MongoDB
$db = ming_db(NULL, $settings);
return $db;
}
/**
* Example 2: Working with data in Ming
*/
function ming_example_working_with_data() {
// Get our MingCoreDatabase object
$db = ming_example_connect();
// Choose a collection to work with
$db->collection('cars');
// Prepare some data
$data = array(
'marque' => 'Rolls Royce',
'model' => 'Silver Shadow',
'year' => '1975',
);
// Insert some data
$db->insert($data);
// Insert some data in a 'safe' way
// This returns an array containing the status of the insert.
// See php.net/manual/en/mongocollection.insert.php for possible
// return values, but note that at some point Ming will do its own error
// handling.
$result = $db->insert($data, TRUE);
// Update the first matched item
// We now have two records for Silver Shadow's, this will only update one.
$data = array(
'marque' => 'Rolls Royce',
'model' => 'Silver Shadow II',
'year' => '1976',
);
$filter = array('marque' => 'Rolls Royce');
$db->update($filter, $data);
// Update all items
// There are still two records, however they are now different. We'll do a
// partial update on both. This will update only the field specified.
$data = array(
'origin' => 'United Kingdom',
);
$db->updateAll($filter, $data, TRUE);
}