文字

MongoDB 类

(PECL mongo >=0.9.0)

简介

该类的实例用于和数据库进行交互。要获取一个数据库:

Example #1 选择一个数据库

<?php

$m 
= new  MongoClient ();  // 连接
$db  $m -> selectDB ( "example" );

?>
数据库名可以用 ASCII 范围内的几乎任何字符。 但是,它们不能包括 " "、".",或者是空字符串。 名称 "system" 也是被保留的。

个别特殊但有效的数据库名:"null"、"[x,y]"、"3"、"\""、 "/"。

不像集合名,数据库名是可以包含 "$" 的。

类摘要

MongoDB {
const int PROFILING_OFF = 0 ;
const int PROFILING_SLOW = 1 ;
const int PROFILING_ON = 2 ;
public integer $w = 1 ;
public integer $wtimeout = 10000 ;
public array authenticate ( string $username , string $password )
public array command ( array $command [, array $options = array() ] )
public __construct ( MongoClient $conn , string $name )
public MongoCollection createCollection ( string $name [, array $options ] )
public array createDBRef ( string $collection , mixed $document_or_id )
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 getCollectionInfo ([ array $options = array() ] )
public array getCollectionNames ([ array $options = array() ] )
public array getDBRef ( array $ref )
public MongoGridFS getGridFS ([ string $prefix = "fs" ] )
public int getProfilingLevel ( void )
public array getReadPreference ( void )
public bool getSlaveOkay ( void )
public array getWriteConcern ( void )
public array lastError ( void )
public array listCollections ([ array $options = array() ] )
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 setReadPreference ( string $read_preference [, array $tags ] )
public bool setSlaveOkay ([ bool $ok = true ] )
public bool setWriteConcern ( mixed $w [, int $wtimeout ] )
public string __toString ( void )
}

预定义常量

MongoDB 日志级别

MongoDB::PROFILING_OFF
0
关闭了分析器。
MongoDB::PROFILING_SLOW
1
为慢操作开启了分析器(>100 ms)。
MongoDB::PROFILING_ON
2
为所有操作开启了分析器。

字段

w
1

在返回成功之前,复制修改到此数量的服务器。 MongoCollection 实例的设置从这里继承。 w 仅仅在 MongoDB 服务器版本 1.5.1+ 以及本驱动 1.0.8+ 有效。

w 用于你需要调整确认级别时 ( MongoCollection::insert() MongoCollection::update() MongoCollection::remove() MongoCollection::save() MongoCollection::ensureIndex() 都支持这个选项)。 默认值(1)情况下,只要数据库有操作就会确认。 如果在复制到从服务器前服务器宕机了,它将可能永久丢失本次操作。 所以,你可以为 w 指定一个比一更高的数字, 在返回成功之前确保至少一个从服务器完成了操作。

例如,如果 w 是 2,主服务器和一个从服务必须记录了本次操作, 否则驱动会抛出 MongoCursorException。 它尝试写入总计 w 个从服务器 + 主服务器,但是如果其中一个从服务器宕机了, 操作也会失败,并会抛出异常,所以通常 w=2 是最安全的(主服务器和一个从服务器)。

wtimeout
10000

等待 MongoDB::$w 复制生效的毫秒数。 MongoCollection 实例的设置从这里继承。 w 仅仅在 MongoDB 服务器版本 1.5.1+ 并且驱动版本 1.0.8+ 有效。

除非设置了 wtimeout,服务器会永久等待复制到 w 个服务器。 这个驱动默认会等待 10 秒,你可以修改这个值来改变它的行为。

参见

MongoDB 关于 » databases 的核心文档。

Table of Contents

  • MongoDB::authenticate — 登录到数据库
  • MongoDB::command — 执行一条 Mongo 指令
  • MongoDB::__construct — 选择一个数据库
  • MongoDB::createCollection — 创建一个集合
  • MongoDB::createDBRef — 创建数据库引用
  • MongoDB::drop — 丢弃数据库
  • MongoDB::dropCollection — Drops a collection [deprecated]
  • MongoDB::execute — 在数据库服务器上运行JavaScript
  • MongoDB::forceError — Creates a database error
  • MongoDB::__get — Gets a collection
  • MongoDB::getCollectionInfo — Returns information about collections in this database
  • MongoDB::getCollectionNames — Gets an array of names for all collections in this database
  • 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::getReadPreference — Get the read preference for this database
  • MongoDB::getSlaveOkay — Get slaveOkay setting for this database
  • MongoDB::getWriteConcern — Get the write concern for this database
  • MongoDB::lastError — Check if there was an error on the most recent db operation performed
  • MongoDB::listCollections — Gets an array of MongoCollection objects for all 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::setReadPreference — Set the read preference for this database
  • MongoDB::setSlaveOkay — Change slaveOkay setting for this database
  • MongoDB::setWriteConcern — Set the write concern for this database
  • MongoDB::__toString — The name of this database

用户评论:

[#1] m dot espositoii at yahoo dot com [2011-08-27 03:11: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.

[#2] jeromakay at yahoo dot com [2011-01-20 12:19:22]

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

上一篇: 下一篇: