略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoDB

2024-04-24

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 ;
/* Fields */
public integer $w = 1 ;
public integer $wtimeout = 10000 ;
/* 方法 */
public authenticate ( string $username , string $password ) : array
public command ( array $command [, array $options = array() ] ) : array
public __construct ( MongoClient $conn , string $name )
public createCollection ( string $name [, array $options ] ) : MongoCollection
public createDBRef ( string $collection , mixed $document_or_id ) : array
public drop ( void ) : array
public dropCollection ( mixed $coll ) : array
public execute ( mixed $code [, array $args = array() ] ) : array
public forceError ( void ) : bool
public __get ( string $name ) : MongoCollection
public getCollectionInfo ([ array $options = array() ] ) : array
public getCollectionNames ([ array $options = array() ] ) : array
public getDBRef ( array $ref ) : array
public getGridFS ([ string $prefix = "fs" ] ) : MongoGridFS
public getProfilingLevel ( void ) : int
public getReadPreference ( void ) : array
public getSlaveOkay ( void ) : bool
public getWriteConcern ( void ) : array
public lastError ( void ) : array
public listCollections ([ array $options = array() ] ) : array
public prevError ( void ) : array
public repair ([ bool $preserve_cloned_files = FALSE [, bool $backup_original_files = FALSE ]] ) : array
public resetError ( void ) : array
public selectCollection ( string $name ) : MongoCollection
public setProfilingLevel ( int $level ) : int
public setReadPreference ( string $read_preference [, array $tags ] ) : bool
public setSlaveOkay ([ bool $ok = TRUE ] ) : bool
public setWriteConcern ( mixed $w [, int $wtimeout ] ) : bool
public __toString ( void ) : string
}

预定义常量

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

add a note add a note

User Contributed Notes 3 notes

up
6
jeromakay at yahoo dot com
8 years ago
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
up
-1
careddumattia at gmail dot com
3 years ago
mongo extension is deprecated.

pecl install mongodb
up
-42
m dot espositoii at yahoo dot com
7 years ago
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.

官方地址:https://www.php.net/manual/en/class.mongodb.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3