V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
lezu
V2EX  ›  MySQL

MongoDB => SQL

  •  
  •   lezu · 2010-12-02 13:26:17 +08:00 · 5085 次点击
    这是一个创建于 5084 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请大家补充——

    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' => '[email protected]');
    mysql_query("INSERT INTO `$tbl` SET `login`='jsmith',`password`='5f4dcc3b5aa765d61d8327deb882cf99',`email`='[email protected]'");
    $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);
    1 条回复    1970-01-01 08:00:00 +08:00
    lezu
        1
    lezu  
    OP
       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'
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2606 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 96ms · UTC 11:34 · PVG 19:34 · LAX 04:34 · JFK 07:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.