downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

MongoDB::authenticate> <Mongo::__toString
[edit] Last updated: Fri, 25 May 2012

view this page in

La classe MongoDB

(No version information available, might only be in SVN)

Introduction

Les objets de cette classe servent à interagir avec la base de données. Pour obtenir une base de données :

<?php

$m 
= new Mongo(); // connexion
$db $m->selectDB("exemple");

?>
Les noms de base de données peuvent contenir n'importe quel caractère ASCII. Mais ils ne peuvent pas contenir les caractères " ", "." ou être la chaîne vide. Le mot "system" est aussi réservé.

Quelques noms de base de données valides mais inattendus : "null", "[x,y]", "3", à"\"", "/".

Contrairement aux noms de collection, les noms de bases de données peuvent contenir "$".

Synopsis de la classe

MongoDB {
/* Constantes */
const int PROFILING_OFF = 0 ;
const int PROFILING_SLOW = 1 ;
const int PROFILING_ON = 2 ;
/* Champs */
public integer $w = 1 ;
public integer $wtimeout = 10000 ;
/* Méthodes */
public array authenticate ( string $username , string $password )
public array command ( array $command [, array $options = array() ] )
public __construct ( Mongo $conn , string $name )
public MongoCollection createCollection ( string $name [, bool $capped = FALSE [, int $size = 0 [, int $max = 0 ]]] )
public array createDBRef ( string $collection , mixed $a )
public array drop ( void )
public array dropCollection ( mixed $coll )
public array execute ( mixed $code [, array $args = array() ] )
public bool forceError ( void )
public MongoCollection __get ( string $name )
public array getDBRef ( array $ref )
public MongoGridFS getGridFS ([ string $prefix = "fs" ] )
public int getProfilingLevel ( void )
public bool getSlaveOkay ( void )
public array lastError ( void )
public array listCollections ( void )
public array prevError ( void )
public array repair ([ bool $preserve_cloned_files = FALSE [, bool $backup_original_files = FALSE ]] )
public array resetError ( void )
public MongoCollection selectCollection ( string $name )
public int setProfilingLevel ( int $level )
public bool setSlaveOkay ([ bool $ok = true ] )
public string __toString ( void )
}

Constantes pré-définies

Niveaux d'historisation de MongoDB

MongoDB::PROFILING_OFF
0
Le profilage est inactif.
MongoDB::PROFILING_SLOW
1
Le profilage est actif pour les opérations lentes (>100 ms).
MongoDB::PROFILING_ON
2
Le profilage est actif sur toutes les opérations.

Champs

w
1

Le nombre de serveurs vers lesquels répliquer avant de retourner avec succès. Hérité par les instances dérivées de la MongoCollection La fonctionnalité w n'est disponible que pour les versions 1.5.1+ du serveur MongoDB et 1.0.8+ du pilote.

w est utilisé chaque fois que vous éxecutez une opération "sécurisée" (MongoCollection::insert(), MongoCollection::update(), MongoCollection::remove(), MongoCollection::save(), et MongoCollection::ensureIndex() supportent tous les options sécurisées). Avec la valeur par défaut (1), une opération sécurisée retournera une fois que le serveur a effectué l'opération. Si le serveur tombe avant que l'opération ne soit répliquée vers un esclave, il est possible de perdre l'opération. Ainsi vous pouvez préciser une valeur supérieure à 1 pour le paramètre w et garantir qu'au moins un esclave recoive l'opération avant qu'elle ne soit considérée comme étant réalisée avec succès.

Par exemple si w vaut 2, le serveur principal et un des esclaves doivent avoir enregistré l'opération ou le pilote enverra une MongoCursorException. Il est tentant de mettre le nombre total d'escalves + le maitre comme numéro pour w, mais alors si un des esclaves tombe l'opération échouera et une exception sera levée, ainsi w=2 est le cas le plus sécurisant (maitre +1esclave).

wtimeout
10000

Le nombre de millisecondes à attendre pour que les réplications de MongoDB::$w démarrent. Hérité par les instances dérivées dans la MongoCollection en cours. La fonctionnalité w n'est disponible que depuis la version 1.5.1+ du serveur MongoDB et 1.0.8+ du pilote.

Sauf si wtimeout est précisé, le serveur attendra indéfiniment que la réplication vers les serveurs w se termine. Le pilote attendra par défaut 10 secondes, vous pouvez changer cette valeur.

Voir aussi

Documentation de MongoDB » concernant les bases de données.

Sommaire



MongoDB::authenticate> <Mongo::__toString
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes MongoDB
m dot espositoii at yahoo dot com 27-Aug-2011 03:11
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.
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?

 
show source | credits | stats | sitemap | contact | advertising | mirror sites