search
Homephp教程php手册PHP连接MongoDB示例

 

 

 

 

PHP连接MongoDB示例:

//这里采用默认连接本机的27017端口,当然你也可以连接远程主机如192.168.0.4:27017,如果端口是27017,端口可以省略

$m = new Mongo();

// 选择comedy数据库,如果以前没该数据库会自动创建,也可以用$m->selectDB("comedy");

$db = $m->comedy;

//选择comedy里面的collection集合,相当于RDBMS里面的表,也-可以使用

$collection = $db->collection;

$db->selectCollection("collection");

//添加一个元素

$obj = array( "title" => "Calvin and Hobbes-".date('i:s'), "author" => "Bill Watterson" );

//将$obj 添加到$collection 集合中

$collection->insert($obj);

//添加另一个元素

$obj = array( "title" => "XKCD-".date('i:s'), "online" => true );

$collection->insert($obj);

//查询所有的记录

$cursor = $collection->find();

//遍历所有集合中的文档

foreach ($cursor as $obj)

{

echo $obj["title"] . "\n";

}

//删除所有数据

$collection->remove();

//删除 name 为hm

$collection->remove(array('name'=>'hm'));

//断开MongoDB连接

$m->close();

?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.