行動 在了實踐之前,希望大家看一下上面的學習資源,了解一下基本操作. 數(shù)據(jù)連接初始賬號密碼 賬號:admin 密碼:admin 首先我們建立一個文件:mongodb.php,設置需要連接賬號密碼,連接數(shù)據(jù)庫 ?php/** * Mongodb Test * * Tools: * Zend Studio 8.x * Eclipse Plug
在了實踐之前,希望大家看一下上面的學習資源,了解一下基本操作.
數(shù)據(jù)連接初始賬號密碼
賬號:admin
密碼:admin
首先我們建立一個文件:mongodb.php,設置需要連接賬號密碼,連接數(shù)據(jù)庫
* * @author Wu Bai Qing* @version $Id: Mongodb.php 17 2011-09-17 06:04:15Z wbq $ */ $mongo_server_name = 'admin'; $mongo_server_pwd = 'admin'; // 連接Mongo數(shù)據(jù)庫 數(shù)據(jù)庫地址:端口/賬號:密碼; $mongo = new Mongo('mongodb://localhost:27017/admin:admin'); // 選擇一個數(shù)據(jù)庫和要操作的集(如果沒有數(shù)據(jù)庫默認創(chuàng)建) $collection = $mongo->selectDB('rrs_result')->selectCollection('content'); ?>
數(shù)據(jù)庫自動創(chuàng)建成功
添加一條數(shù)據(jù)!
'葉子-吳佰清', 'author'=>'吳佰清', 'url'=>'http://www.cnblogs.com/wubaiqing/archive/2011/09/17/2179870.html', ); $collection->insert($content); ?>
存放的數(shù)據(jù)內(nèi)容:
<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+CrLp0a/L+dPQyv2+3To8L3A+Cgo8aW1nIHNyYz0="/Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s/new/2019-09-30-201930/0514502151-0.gif" alt="">
find(); foreach ($rows as $key => $val) { var_dump($val); } ?>
結(jié)果:
更改數(shù)據(jù):
'葉子-吳佰清'); $set = array('title'=>'葉子'); $collection->update($where,array( '$set'=>$set, )); ?>
結(jié)果:
刪除數(shù)據(jù):
remove(array( 'title'=>'葉子', )); ?>
結(jié)果:
No records is found.
完整代碼:
* @version $Id: Mongodb.php 17 2011-09-17 06:04:15Z wbq $ */ $mongo_server_name = 'admin'; $mongo_server_pwd = 'admin'; // 連接Mongo數(shù)據(jù)庫 數(shù)據(jù)庫地址:端口/賬號:密碼; $mongo = new Mongo('mongodb://localhost:27017/admin:admin'); // 選擇一個數(shù)據(jù)庫和要操作的集(如果沒有數(shù)據(jù)庫默認創(chuàng)建) $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")); |
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com