MongoDB => SQL

2010-12-02 13:26:17 +08:00
 lezu
请大家补充——

Connecting, Creating and using a DB, Create a Table / Collection
$link = new Mongo(); $link = mysql_connect($host, $user, $pass, 1) or die("Could not connect to: {$host}.");
$db = $link->testdb; $sql = "CREATE DATABASE `$db`"; mysql_select_db($db`,$link);
$col = $db->user; $sql = "CREATE TABLE 'user'...; mysql_query($sql,$link);
$col->drop(); mysql_query(DROP TABLE `$db`.`$tbl`, $link);
$db->drop(); mysql_query(DROP DATABASE `$db`, $link);
$link->close(); mysql_close($link);
Insert Data
$doc= array('login' => 'jsmith', 'password' => ' 5f4dcc3b5aa765d61d8327deb882cf99', 'email' => 'jsmith@example.com');
mysql_query("INSERT INTO `$tbl` SET `login`='jsmith',`password`='5f4dcc3b5aa765d61d8327deb882cf99',`email`='jsmith@example.com'");
$id = $doc['_id']; $id = mysql_insert_id($link);
Updating Data
$col->update(array('_id' => $id), array('$set' => array('password' => 'b497dd1a701a33026f7211533620780d')));
$qry = mysql_query("UPDATE `$tbl` SET `password` = 'b497dd1a701a33026f7211533620780d' WHERE `id` = {$id}", $link);
Indexing data
$col->ensureIndex(array("login" => 1), array("unique" => true, "dropDups" => true));
$qry = mysql_query("ALTER TABLE `$db`.`$tbl` ADD UNIQUE `login` (`login`)", $link);
5102 次点击
所在节点    MySQL
1 条回复
lezu
2010-12-02 13:26:22 +08:00
Querying Data
$res = $col->find(); SELECT * FROM `$tbl`
$doc = $col->findone(array('_id' => $id)); SELECT * FROM `$tbl` WHERE `id` = {$id}
$res = $col->find()->limit(1); SELECT * FROM `$tbl` LIMIT 1
$res = $col->find()->skip(1)->limit(1); SELECT * FROM `$tbl` OFFSET 1 LIMIT 1;
$res = $col->find()->sort(array('login' => -1)); SELECT * FROM `$tbl` ORDERBY `login` DESC //-1=DESC,1=ASC
$res = $col->find(array('age' => array('$lt' => 30))); SELECT * FROM `$tbl` WHERE `age` < 30 //$lt:<,$gt:>,$gte:>=, $lte:<=, $ne:!=
$doc = $col->find(array(’a'=>’hello’,'b’=>1)); SELECT * FROM `$tbl` WHERE `a` = ‘hello’ AND `b`=1
$doc = $col->find(array(’a'=>’hello’,'b’=>array(’$gt’=>1))); SELECT * FROM `$tbl` WHERE `a` = ‘hello’ AND `b`>=1
$res = $col->find(array('age' => array('$gte' => 20, '$lte' => 50))); SELECT * FROM `$tbl` WHERE `age` >= 20 AND `age` <= 50
$col->remove(array('age'=>24),array('justOne'=>true,'safe'=>true)); DELETE FROM `$tbl` WHERE `login` = 'psmith'

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/5296

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX