标签存档: mongo

NOSQL mongodb与php

第一个mongodb与php测试用例:

<?php
try {
$conn = new Mongo();
}catch(Exception $e){
die('Fail');
}

$db = $conn->test;

//mongo认证,返回array([ok]=>1)表示认证成功
print_r($db->authenticate("admin", "123456"));

$cursor = $db->data->find();

while ($cursor->hasNext()){
$result[] = $cursor->getNext();
}
print_r($result);

增删改查:

<?php
try {
$conn = new Mongo();
}catch(Exception $e){
die('Fail');
}

$db = $conn->test;

$db->authenticate("admin", "123456");

$collection = $db->data;

//新增
$user = array('name' => 'admin', 'email' => 'admin@admin.com');
$collection->insert($user); 

//删除
$collection->remove(array('name'=>'test'), array("email" => "admin@admin.com"));  

//修改
$newdata = array('$set' => array("email" => "test@test.com"));
$collection->update(array("name" => "admin"), $newdata);

//查找一条
$user = $collection->findOne(array('name' => 'admin'), array('email'));  //查找name=admin的某一行中的email列值
var_dump($user);
第 1 页,共 1 页1