colinmollenhour / mongodb-php-odm

A simple but powerful set of wrappers for using MongoDb in PHP (also a Kohana 3 module)
BSD 3-Clause "New" or "Revised" License
208 stars 50 forks source link

Capped collections #47

Closed sergeyklay closed 11 years ago

sergeyklay commented 11 years ago

Hi!

How can I get new collection by Mongo_Database::__get($name) with custom options?

Obviously, when creating the collection we can point explicitly:

$collection = $db->command_safe(
  array(
    "create" => $collection_name
    "size"   => $collection_size,
    "capped" => $capped,
    "max"    => $max_size
  )
);

Based on the official documentation of the parameters have the following meaning:

capped

If the collection should be a fixed size;

size

If the collection is fixed size, its size in bytes;

max

If the collection is fixed size, the maximum number of elements to store in the collection;

autoIndexId

If capped is TRUE you can specify FALSE to disable the automatic index created on the _id field. Before MongoDB 2.2, the default value for autoIndexId was false;

But I frankly do not yet quite understand how to use Mongo_Database::__get($name) to get (create) the new collection with the specified parameters where the object is an instance of the Mongo_Colleaction class...

Best regards,

colinmollenhour commented 11 years ago

I think you answered your own question..

sergeyklay commented 11 years ago

But I don't quite understand how do call Mongo_Database::__get($name) and get a collection created with arbitrary options, e.g. as described above.

It turns out must first create a collection, and then get it?

sergeyklay commented 11 years ago

In order to create a collectionneed to first determine whether it exists or not. I propose to put the following code into the class:

/**
 * Checks for the $collection in the currently referenced database
 *
 * @param   string  $collection  Collection name
 * @return  boolean
 */
public function exists($collection)
{
    $result = $this->db()->system->namespaces->findOne(
        array('name'=>"{$this->_config['database']}." . $collection)
    );

    return ( ! is_null($result));
}

or something like that

For adition information please see http://docs.mongodb.org/manual/reference/system-collections/

sergeyklay commented 11 years ago

P.S. I am sorry for this syntax I'm not sure that at this stage the database name can be obtained easier