zoukankan      html  css  js  c++  java
  • 第二天用MongoDB 简单

    <?php
    header("Content-type: text/html; charset=utf-8");
    
    
    $mo = new Mongo();
    $db = $mo->comedy;
    $collection = $db->collection;
    $user = $db->user;
    
    
    echo "<h1>插入</h1>";
    //添加一个元素
    //$obj = array("title"=>"这是标题", "author"=>"作者向晓东");
    //$collection->insert($obj);
    
    //添加另外一个元素
    //$obj = array("title"=>"XKCD", "online"=>true);
    //$collection->insert($obj);
    /*$cursor = $collection->find();
    var_dump($cursor);
    foreach($cursor as $obj){
        echo $obj['title']."<BR><BR>";
        if(isset($obj['author'])){
           echo $obj['author']."<BR><BR>";
        }
        if(isset($obj['online'])){
           echo $obj['online']."<BR><BR>";
        }
    }
    //$mo->close();   //关闭链接*/
    echo "<h3>插入一条语句到user集合中</h3>";
    echo "INSERT INTO USERS VALUES(1,1)<BR>";
    //$db->user->insert(array("name"=>"xianglingchuan", "age"=>33));
    
    
    
    
    echo "<h1>查询</h1>";
    
    echo "<h3>查询 title=‘XKCD’ </h3>";
    $query = array("title"=>"XKCD");
    $cursor = $collection->find($query);
    foreach ($cursor as $key => $value) {
         echo "title:".$value['title']."<BR>";
         echo "id:".$value['_id']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询 整个积合中的第一个文档 </h3>";
    $one = $collection->findOne();
    echo "title:".$one['title']."<BR>";
    echo "id:".$one['_id']."<BR>";
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询 整个积合记录总数 </h3>";
    $count = $collection->count();
    echo "总记录数为:{$count}<BR>";
    echo "================================<BR><BR>";
    
    echo "<h3>查询 积合的指定字段title </h3>";
    $cursor = $collection->find(array(), array("title"=>1));
    foreach ($cursor as $key => $value) {
        echo "title:".$value['title']."<BR>";
        //echo "id:".$value['_id']."<BR>"; id是什么时候都可以访问的
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询 积合的指定字段title进行排序查询 </h3>";
    $cursor = $collection->find(array(), array("title"=>1))->sort(array("title"=>1));
    foreach ($cursor as $key => $value) {
        echo "title:".$value['title']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 所有字段,年龄为33 </h3>";
    echo "SELECT * FROM users WHERE age=33<BR>";
    $cursor = $user->find(array("age"=>33));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']."<BR>";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,年龄为33 </h3>";
    echo "SELECT name FROM users WHERE age=33<BR>";
    $cursor = $user->find(array("age"=>33), array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,对name进行升排序 </h3>";
    echo "SELECT name, age FROM users ORDER BY name ASC<BR>";
    $cursor = $user->find(array(), array("name"=>1, "age"=>1))->sort(array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user 指定字段,age>21 </h3>";
    echo "SELECT * FROM users WHERE age>21<BR>";
    $cursor = $user->find(array("age"=>array('$gt'=>21)), array("name"=>1, "age"=>1))->sort(array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user 指定字段,age<33 </h3>";
    echo "SELECT * FROM users WHERE age<33<BR>";
    $cursor = $user->find(array("age"=>array('$lt'=>33)), array("name"=>1, "age"=>1))->sort(array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,LIKE \"%test%\" </h3>";
    echo "SELECT * FROM users WHERE name LIKE \"%test%\"<BR>";
    $cursor = $user->find(array("name"=>new MongoRegex("/test/")), array("name"=>1, "age"=>1))->sort(array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,LIKE \"x%\" </h3>";
    echo "SELECT * FROM users WHERE name LIKE \"x%\"<BR>";
    $cursor = $user->find(array("name"=>new MongoRegex("/^x/")), array("name"=>1, "age"=>1))->sort(array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,age>33 AND age<=55 </h3>";
    echo "SELECT * FROM users WHERE age>33 AND age<=55 <BR>";
    $cursor = $user->find(array("age"=>array('$gt'=>33, '$lte'=>55)), array("name"=>1, "age"=>1))->sort(array("name"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,ORDER BY name DESC </h3>";
    echo "SELECT * FROM users ORDER BY name DESC <BR>";
    $cursor = $user->find(array(), array("name"=>1, "age"=>1))->sort(array("name"=>-1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user CREATE INDEX myindexname ON users(name) 不懂</h3>";
    
    echo "<h3>查询user CREATE INDEX myindexname ON users(name) 不懂</h3>";
    
    
    
    echo "<h3>查询user 指定字段,name='hello' AND age='32' </h3>";
    echo "SELECT * FROM users WHERE name='hello' AND age='32' <BR>";
    $cursor = $user->find(array("name"=>"hello", "age"=>32), array("name"=>1, "age"=>1))->sort(array("name"=>-1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user 指定字段,LIMIT 3 SKIP 2 </h3>";
    echo "SELECT * FROM users LIMIT 3 SKIP 2<BR>";
    $cursor = $user->find(array(), array("name"=>1, "age"=>1))->limit(3)->skip(2);
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user 指定字段,name='hello' or age=55 </h3>";
    echo "SELECT * FROM users WHERE name='hello' or age=55<BR>";
    $cursor = $user->find(array('$or'=>array(array('name'=>'hello'), array('age'=>55))), array("name"=>1, "age"=>1));
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,limit 1 </h3>";
    echo "SELECT * FROM users limit 1<BR>";
    $cursor = $user->find(array('$or'=>array(array('name'=>'hello'), array('age'=>55))), array("name"=>1, "age"=>1))->limit(1);
    foreach ($cursor as $key => $value) {
        echo "name:".$value['name']." ";
        echo "age:".$value['age']."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user EXPLAIN SELECT * FROM users WHERE name='hello' 不懂</h3>";
    echo "<BR>";
    $cursor = $user->find(array('age'=>32))->explain();
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>查询user 指定字段,DISTINCT name </h3>";
    echo "SELECT DISTINCT name FROM users<BR>";
    $cursor = $db->command(array("distinct"=>"user", "key"=>"name"));
    $values = $cursor['values'];
    foreach ($values as $key => $value) {
        echo "name:".$value."<BR>";
    }
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user count(*)</h3>";
    echo "SELECT COUNT(*) FROM users<BR>";
    $count = $db->user->count();
    echo "总记录数为:{$count}<BR>";
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user count(*) where age>32</h3>";
    echo "SELECT COUNT(*y) FROM users where AGE > 32 ";
    $count = $user->find(array("age"=>array('$gt'=>32)))->count();
    echo "<BR>总记录数为:{$count}<BR>";
    echo "================================<BR><BR>";
    
    
    echo "<h3>查询user count(age)</h3>";
    echo "SELECT COUNT(AGE) from users <BR>";
    $count = $user->find(array("age"=>array('$exists'=>true)))->count();
    echo "<BR>总记录数为:{$count}<BR>";
    echo "================================<BR><BR>";
    
    
    echo "<h3>更新user age=25 WHERE name='xianglingchuan'</h3>";
    echo "UPDATE users SET age=25 WHERE name='xianglingchuan' <BR>";
    $result = $user->update(array('name'=>'xianglingchuan'), array('$set'=>array("age"=>25)));
    var_dump($result);
    echo "================================<BR><BR>";
    
    
    echo "<h3>更新user age=age+2 WHERE name='xianglingchuan'</h3>";
    echo "UPDATE users SET age=age+2 WHERE name='xianglingchuan' <BR>";
    $result = $user->update(array('name'=>'xianglingchuan'), array('$inc'=>array("age"=>2)));
    var_dump($result);
    echo "================================<BR><BR>";
    
    
    
    echo "<h3>删除user  name='xlc'</h3>";
    echo "DELETE FROM users WHERE z='abc' <BR>";
    $result = $user->remove(array('name'=>'xlc'));
    var_dump($result);
    echo "================================<BR><BR>";
    
    
    echo "<BR><BR>";
    ?>
    

      

  • 相关阅读:
    微信小程序 editor富文本
    vuex详解
    每日一练
    如何有效地进行代码 Review?
    vue实现瀑布流
    浅谈js防抖和节流-转载
    Java Kafka 消费积压监控
    用于文本内容的复制粘贴
    Java 键值对数据本地保存与读取
    Java ElasticSearch 操作
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2815549.html
Copyright © 2011-2022 走看看