With Mongo it'll automatically create the collection, so just start using it and it'll do the creation itself.
In other words... just use SelectCollection, if it doesn't exist, it will after that so you can drop it.
The MongoDB class
Увод
Instances of this class are used to interact with a database. To get a database:
<?php
$m = new Mongo(); // connect
$db = $m->selectDB("example");
?>
A few unusual, but valid, database names: "null", "[x,y]", "3", "\"", "/".
Unlike collection names, database names may contain "$".
Синтаксис за класове
MongoDB
MongoDB
{
/* Methods */
public MongoCollection createCollection
( string $name
[, bool $capped = FALSE
[, int $size = 0
[, int $max = 0
]]] )
public array repair
([ bool $preserve_cloned_files = FALSE
[, bool $backup_original_files = FALSE
]] )
}Съдържание
- MongoDB::command — Execute a database command
- MongoDB::__construct — Creates a new database
- MongoDB::createCollection — Creates a collection
- MongoDB::createDBRef — Creates a database reference
- MongoDB::drop — Drops this database
- MongoDB::dropCollection — Drops a collection
- MongoDB::execute — Runs JavaScript code on the database server.
- MongoDB::forceError — Creates a database error
- MongoDB::getCursorInfo — Gets information from the database about cursors
- MongoDB::getDBRef — Fetches the document pointed to by a database reference
- MongoDB::getGridFS — Fetches toolkit for dealing with files stored in this database
- MongoDB::getProfilingLevel — Gets this database's profiling level
- MongoDB::lastError — Check if there was an error on the most recent db operation performed
- MongoDB::listCollections — Get a list of collections in this database
- MongoDB::prevError — Checks for the last error thrown during a database operation
- MongoDB::repair — Repairs and compacts this database
- MongoDB::resetError — Clears any flagged errors on the database
- MongoDB::selectCollection — Gets a collection
- MongoDB::setProfilingLevel — Sets this database's profiling level
- MongoDB::__toString — The name of this database
m dot espositoii at yahoo dot com
27-Aug-2011 03:11
jeromakay at yahoo dot com
20-Jan-2011 12:19
based on what I've read and then applied, you don't have to specifically create a database or table, you just initialize it.
Indeed, files are not being written inside /data/db, but they will the first moment you start adding data.
So, I'm taking as an example Twitter, with no db defined, I'm still going to have the db available if I run this code:
<?php
define('TWITTER_API_VERSION', 1);
date_default_timezone_set("Europe/Dublin");
try
{
$m = new Mongo(); // connect
$db = $m->selectDB("example");
}
catch ( MongoConnectionException $e )
{
echo '<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';
exit();
}
$updates = file_get_contents( "http://api.twitter.com/". TWITTER_API_VERSION ."/statuses/public_timeline.json" );
$updates = json_decode( $updates );
if ( $updates && is_array( $updates ) && count( $updates ) )
{
foreach ( $updates as $update )
{
$db->users->insert( $update );
}
}
?>
Hope this was helpful!
Good luck!
Vladimir Ghetau
jonwage at gmail dot com
01-May-2010 11:56
Just a note that if you use selectDB() and only select it, the database will not be created. In PHPMongoDBAdmin(http://github.com/jwage/php-mongodb-admin) I wanted a way to create a database through a form so I needed to create a dummy collection and drop it in order for the database to be created. MongoDB has a drop() method but no create() method.
<?php
$mongo->selectDB('db')->createCollection('__tmp_collection_');
$mongo->selectDB('db')->dropCollection('__tmp_collection_');
?>
Any better way to do this?
