• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

初识Mongodb之[CURD]-PHP版

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

导读:需要搭建好Mangodb的环境,没有搭建好的请看下:初识Mongodb总结

边看边操作,这样才能记得牢,实践是检验真理的唯一标准.

光看不练假把式,光练不看傻把式,边看边练真把式.

知识改变命运,为了我们的未来,为了我们的梦想,一起动起来吧!


转载请著原信息:

作者:吴佰清

地址:http://www.cnblogs.com/wubaiqing/archive/2011/09/17/2179870.html

开发环境


操作系统  : Mac Os X
集成环境  : Mamp
编程语言  : PHP
Server    : Mongodb
Extension   : Mongo-1.2.4
管理工具  : Rock-PHP

 

行动

在了实践之前,希望大家看一下上面的学习资源,了解一下基本操作.

数据连接初始账号密码

账号:admin

密码:admin
 

首先我们建立一个文件:mongodb.php,设置需要连接账号密码,连接数据库

<?php
/**
* Mongodb Test
*
* Tools:
* Zend Studio 8.x
* Eclipse Pl
ug :Toggle Varpper
 * 
* @author Wu Bai Qing<[email protected]>
* @version $Id: Mongodb.php 17 2011-09-17 06:04:15Z wbq $
*/

$mongo_server_name = 'admin';
$mongo_server_pwd = 'admin';

// 连接Mongo数据库 数据库地址:端口/账号:密码;
$mongo = new Mongo('mongodb://localhost:27017/admin:admin');

// 选择一个数据库和要操作的集(如果没有数据库默认创建)
$collection = $mongo->selectDB('rrs_result')->selectCollection('content');
?>

数据库自动创建成功


添加一条数据!

<?php
$content = array(
'title'=>'叶子-吴佰清',
'author'=>'吴佰清',
'url'=>'http://www.cnblogs.com/wubaiqing/archive/2011/09/17/2179870.html',
);
$collection->insert($content);
?>

存放的数据内容:



查询所有数据:

<?php
$rows = $collection->find();

foreach ($rows as $key => $val)
{
var_dump($val);
}
?>

结果:


更改数据:

<?php
$where = array('title'=>'叶子-吴佰清');
$set = array('title'=>'叶子');

$collection->update($where,array(
'$set'=>$set,
));
?>

结果:




删除数据:

<?php
$collection->remove(array(
'title'=>'叶子',
));
?>

结果:

No records is found.
 

完整代码:

<?php
/**
* Mongodb Test
*
* Tools:
* Zend Studio 8.x
* Eclipse Pulg :Toggle Varpper
*
* @author Wu Bai Qing<[email protected]>
* @version $Id: Mongodb.php 17 2011-09-17 06:04:15Z wbq $
*/

$mongo_server_name = 'admin';
$mongo_server_pwd = 'admin';

// 连接Mongo数据库 数据库地址:端口/账号:密码;
$mongo = new Mongo('mongodb://localhost:27017/admin:admin');

// 选择一个数据库和要操作的集(如果没有数据库默认创建)
$collection = $mongo->selectDB('rrs_result')->selectCollection('content');


/*
// 添加
$content = array(
'title'=>'叶子-吴佰清',
'author'=>'吴佰清',
'url'=>'http://www.cnblogs.com/wubaiqing/archive/2011/09/17/2179870.html',
);
$collection->insert($content);
*/

/*
// 查询
$colle = $collection->find(array('title'=>'叶子-吴佰清'));

foreach ($colle as $key => $val)
{
var_dump($val);
}
*/

/*
// 修改
$where = array('title'=>'叶子-吴佰清');
$set = array('title'=>'叶子');

$collection->update($where,array(
'$set'=>$set,
));
*/

/*
//删除
$collection->remove(array(
'title'=>'叶子',
));
*/


// End 2011-09-17 23:39
?>

  

 

下面是sql和Mongodb对应的一些语法:

SQL Statement Mongo Query Language Statement
CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB::createCollection().
INSERT INTO USERS VALUES(1,1) $db->users->insert(array("a" => 1, "b" => 1));
SELECT a,b FROM users $db->users->find(array(), array("a" => 1, "b" => 1));
SELECT * FROM users WHERE age=33 $db->users->find(array("age" => 33));
SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
SELECT a,b FROM users WHERE age=33 ORDER BY name $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1));
SELECT * FROM users WHERE age>33 $db->users->find(array("age" => array('$gt' => 33)));
SELECT * FROM users WHERE age<33 $db->users->find(array("age" => array('$lt' => 33)));
SELECT * FROM users WHERE name LIKE "%Joe%" $db->users->find(array("name" => new MongoRegex("/Joe/")));
SELECT * FROM users WHERE name LIKE "Joe%" $db->users->find(array("name" => new MongoRegex("/^Joe/")));
SELECT * FROM users WHERE age>33 AND age<=40 $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
SELECT * FROM users ORDER BY name DESC $db->users->find()->sort(array("name" => -1));
CREATE INDEX myindexname ON users(name) $db->users->ensureIndex(array("name" => 1));
CREATE INDEX myindexname ON users(name,ts DESC) $db->users->ensureIndex(array("name" => 1, "ts" => -1));
SELECT * FROM users WHERE a=1 and b='q' $db->users->find(array("a" => 1, "b" => "q"));
SELECT * FROM users LIMIT 10 SKIP 20 $db->users->find()->limit(10)->skip(20);
SELECT * FROM users WHERE a=1 or b=2 $db->users->find(array('$or' => array(array("a" => 1), array("b" => 2))));
SELECT * FROM users LIMIT 1 $db->users->find()->limit(1);
EXPLAIN SELECT * FROM users WHERE z=3 $db->users->find(array("z" => 3))->explain()
SELECT DISTINCT last_name FROM users $db->command(array("distinct" => "users", "key" => "last_name"));
SELECT COUNT(*y) FROM users $db->users->count();
SELECT COUNT(*y) FROM users where AGE > 30 $db->users->find(array("age" => array('$gt' => 30)))->count();
SELECT COUNT(AGE) from users $db->users->find(array("age" => array('$exists' => true)))->count();
UPDATE users SET a=1 WHERE b='q' $db->users->update(array("b" => "q"), array('$set' => array("a" => 1)));
UPDATE users SET a=a+2 WHERE b='q' $db->users->update(array("b" => "q"), array('$inc => array("a" => 2)));
DELETE FROM users WHERE z="abc" $db->users->remove(array("z" => "abc"));


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php数组遍历顺序原理(转)发布时间:2022-07-10
下一篇:
Linux——搭建PHP开发环境第三步:mysql发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap