mongodb select php操作 命令行操作

下面说一下,mongodb select的常用操作

测试数据

查看复制打印?

  1. { "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }
  2. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
  3. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  4. { "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

1,取表条数

查看复制打印?

  1. > db.books.count();
  2. 4
  3. > db.books.find().count();
  4. 4
  5. > db.books.count({auther: "李白" });
  6. 2
  7. > db.books.find({money:{$gt:40,$lte:60}}).count();
  8. 1
  9. > db.books.count({money:{$gt:40,$lte:60}});
  10. 1

php代码如下,按顺序对应的

查看复制打印?

  1. $collection->count();             //结果:4
  2. $collection->find()->count();     //结果:4
  3. $collection->count(array("auther"=>"李白"));   //结果:2
  4. $collection->find(array("money"=>array(‘$gt‘=>40,‘$lte‘=>60)))->count();     //结果:1
  5. $collection->count(array("money"=>array(‘$gt‘=>40,‘$lte‘=>60)));    //结果:1

提示:$gt为大于、$gte为大于等于、$lt为小于、$lte为小于等于、$ne为不等于、$exists不存在、$in指定范围、$nin指定不在某范围

2,取单条数据

查看复制打印?

  1. > db.books.findOne();
  2. {
  3. "_id" : 1,
  4. "title" : "红楼梦",
  5. "auther" : "曹雪芹",
  6. "typeColumn" : "test",
  7. "money" : 80,
  8. "code" : 10
  9. }
  10. > db.books.findOne({auther: "李白" });
  11. {
  12. "_id" : 3,
  13. "title" : "朝发白帝城",
  14. "auther" : "李白",
  15. "typeColumn" : "test",
  16. "money" : 30,
  17. "code" : 30
  18. }

php代码如下,按顺序对应的

查看复制打印?

  1. $collection->findOne();
  2. $collection->findOne(array("auther"=>"李白"));

3,find snapshot 游标

查看复制打印?

  1. > db.books.find( { $query: {auther: "李白" }, $snapshot: true } );
  2. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  3. { "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代码如下

查看复制打印?

  1. /**
  2. * 注意:
  3. * 在我们做了find()操作,获得 $result 游标之后,这个游标还是动态的.
  4. * 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$result 获得.
  5. */
  6. $result = $collection->find(array("auther"=>"李白"))->snapshot();
  7. foreach ($result as $id => $value) {
  8. var_dump($value);
  9. }

4,自定义列显示

查看复制打印?

  1. > db.books.find({},{"money":0,"auther":0});      //money和auther不显示
  2. { "_id" : 1, "title" : "红楼梦", "typeColumn" : "test", "code" : 10 }
  3. { "_id" : 2, "title" : "围城", "typeColumn" : "test", "code" : 20 }
  4. { "_id" : 3, "title" : "朝发白帝城", "typeColumn" : "test", "code" : 30 }
  5. { "_id" : 4, "title" : "将近酒", "code" : 40 }
  6. > db.books.find({},{"title":1});          //只显示title列
  7. { "_id" : 1, "title" : "红楼梦" }
  8. { "_id" : 2, "title" : "围城" }
  9. { "_id" : 3, "title" : "朝发白帝城" }
  10. { "_id" : 4, "title" : "将近酒" }
  11. /**
  12. *money在60到100之间,typecolumn和money二列必须存在
  13. */
  14. > db.books.find({money:{$gt:60,$lte:100}},{"typeColumn":1,"money":1});
  15. { "_id" : 1, "typeColumn" : "test", "money" : 80 }
  16. { "_id" : 4, "money" : 90 }

php代码如下,按顺序对应的

查看复制打印?

  1. $result = $collection->find()->fields(array("auther"=>false,"money"=>false));    //不显示auther和money列
  2. $result = $collection->find()->fields(array("title"=>true));      //只显示title列
  3. /**
  4. *money在60到100之间,typecolumn和money二列必须存在
  5. */
  6. $where=array(‘typeColumn‘=>array(‘$exists‘=>true),‘money‘=>array(‘$exists‘=>true,‘$gte‘=>60,‘$lte‘=>100));
  7. $result = $collection->find($where);

5,分页

查看复制打印?

  1. > db.books.find().skip(1).limit(1);  //跳过第条,取一条
  2. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }

这根mysql,limit,offset有点类似,php代码如下

查看复制打印?

  1. $result = $collection->find()->limit(1)->skip(1);//跳过 1 条记录,取出 1条

6,排序

查看复制打印?

  1. > db.books.find().sort({money:1,code:-1});    //1表示降序 -1表示升序,参数的先后影响排序顺序
  2. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  3. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
  4. { "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }
  5. { "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代码如下

查看复制打印?

  1. $result = $collection->find()->sort(array(‘code‘=>1,‘money‘=>-1));

7,模糊查询

查看复制打印?

  1. > db.books.find({"title":/城/});      //like ‘%str%‘ 糊查询集合中的数据
  2. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
  3. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  4. > db.books.find({"auther":/^李/});    //like ‘str%‘ 糊查询集合中的数据
  5. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  6. { "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }
  7. > db.books.find({"auther":/书$/});   //like ‘%str‘ 糊查询集合中的数据
  8. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
  9. > db.books.find( { "title": { $regex: ‘城‘, $options: ‘i‘ } } );   //like ‘%str%‘ 糊查询集合中的数据
  10. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
  11. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }

php代码如下,按顺序对应的

查看复制打印?

  1. $param = array("title" => new MongoRegex(‘/城/‘));
  2. $result = $collection->find($param);
  3. $param = array("auther" => new MongoRegex(‘/^李/‘));
  4. $result = $collection->find($param);
  5. $param = array("auther" => new MongoRegex(‘/书$/‘));
  6. $result = $collection->find($param);

8,$in和$nin

查看复制打印?

  1. > db.books.find( { money: { $in: [ 20,30,90] } } );   //查找money等于20,30,90的数据
  2. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  3. { "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }
  4. > db.books.find( { auther: { $in: [ /^李/,/^钱/ ] } } );    //查找以李,钱开头的数据
  5. { "_id" : 2, "title" : "围城", "auther" : "钱钟书", "typeColumn" : "test", "money" : 56, "code" : 20 }
  6. { "_id" : 3, "title" : "朝发白帝城", "auther" : "李白", "typeColumn" : "test", "money" : 30, "code" : 30 }
  7. { "_id" : 4, "title" : "将近酒", "auther" : "李白", "money" : 90, "code" : 40 }

php代码如下,按顺序对应的

查看复制打印?

  1. $param = array("money" => array(‘$in‘=>array(20,30,90)));
  2. $result = $collection->find($param);
  3. foreach ($result as $id=>$value) {
  4. var_dump($value);
  5. }
  6. $param = array("auther" => array(‘$in‘=>array(new MongoRegex(‘/^李/‘),new MongoRegex(‘/^钱/‘))));
  7. $result = $collection->find($param);
  8. foreach ($result as $id=>$value) {
  9. var_dump($value);
  10. }

 9,$or

查看复制打印?

  1. > db.books.find( { $or: [ { money: 20 }, { money: 80 } ] } );   //查找money等于20,80的数据
  2. { "_id" : 1, "title" : "红楼梦", "auther" : "曹雪芹", "typeColumn" : "test", "money" : 80, "code" : 10 }

php代码如下

查看复制打印?

  1. $param = array(‘$or‘=>array(array("money"=>20),array("money"=>80)));
  2. $result = $collection->find($param);
  3. foreach ($result as $id=>$value) {
  4. var_dump($value);
  5. }

 10,distinct

查看复制打印?

  1. > db.books.distinct( ‘auther‘ );
  2. [ "曹雪芹", "钱钟书", "李白" ]
  3. > db.books.distinct( ‘auther‘ , { money: { $gt: 60 } });
  4. [ "曹雪芹", "李白" ]

php代码如下

查看复制打印?

  1. $result = $curDB->command(array("distinct" => "books", "key" => "auther"));
  2. foreach ($result as $id=>$value) {
  3. var_dump($value);
  4. }
  5. $where = array("money" => array(‘$gte‘ => 60));
  6. $result = $curDB->command(array("distinct" => "books", "key" => "auther", "query" => $where));
  7. foreach ($result as $id=>$value) {
  8. var_dump($value);
  9. }

先写到这儿,上面只是SELECT的一些常用操作,接下来,还会写一点。

时间: 2024-08-08 07:10:32

mongodb select php操作 命令行操作的相关文章

MongoDB命令行操作

1. 连接MongoDB: Mongodb://username:[email protected]/dbname 2. 创建数据库: use dbname:如果数据库不存在则创建数据库,否则切换到指定数据库. 3. 查看所有数据库: show dbs 注: 该命令不会显示新创建的空数据库,若想显示需要向空数据库插入一些数据. MongoDB中默认的数据库为test,若果没有创建新的数据库,集合将存放于test中. 4. 查看当前数据库名: db 5. 删除数据库: db.dropDatabas

SQL命令行操作

命令行操作(mysql.exe)    0.登录  :       mysql -u root -p    1.显示数据库列表:    show databases;     2.选择数据库:      use 库名;    3.显示数据表列表     show tables;    4.显示数据表的结构: desc 表名;    5.建库:       create database 库名 charset utf8;    6.建表:       use 库名:     create tabl

android 使用命令行操作模拟器

首先你得配置好环境变量,不懂配置或者没配置好的去别处查查先,这里就不教了.建议是否配置好环境变量的方法就是打开运行窗,输入 cmd 然后回车,如果输出一大堆东西就说明配置好了. 然后这个什么叫做命令行操作模拟器呢,主要有个用途比较实在,就是安装一些应用程序到模拟器上.或者是查看手机上面的一些数据,尤其是数据库: 一些功能操作在这里列出来: 1.利用命令行安装手机软件到模拟器上 adb install path 其中path为路径名比如我在d盘有个文件夹app里面有个软件a.apk则path为:d

2015.12.01 软件安装 命令行操作 vi

软件安装 1.App Store 2..dmg/.pkg(相当于光盘镜像) 双击安装 3.绿色软件,*.app 直接拖拽到Application (非官方的安装,要注意在偏好设置中允许任何来源) 常用网址:www.macx.cn        bbs.feng.com 在对MacOS系统的操作当中,有两种操作方式.一种就是图形化操作,另一种就是命令行操作.对比而言,前者更直观,而后者则是更便捷并且节约资源. 基本命令 ls                           查看当前文件夹下的文

ubuntu命令行操作mysql常用操作

登陆mysql [email protected]:~/ruby/mydiary$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. 查看所有的数据库 mysql> show databases; --注意必须要用;结尾否则不会立即执行代码 +--------------------+ | Database | +--------------------+ | inf

github linux 命令行操作实例

继续整理一下linux 下面使用命令行操作实例 首先创建文件目录 然后 执行 git clone 操作 [email protected]:~/桌面$ cd test/ [email protected]:~/桌面/test$ git clone https://github.com/timelessz/TESTDEMO.git正克隆到 'TESTDEMO'...remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused

win7休眠的开启与关闭方法命令行操作和图文结合的鼠标操作

win7休眠的开启与关闭方法 从开始菜单中找到"附件→命令提示符",手工输入如下命令:powercfg -a,从这里可以清楚的看到,计算机是支持休眠的,显示"尚未启用休眠".仍然在命令提示符下进行操作,     开始休眠方法:手工键入如下命令: powercfg -hibernate on(关闭则为powercfg -hibernate off) 命令执行之后立即就可以生效,无需要重新启动系统,再次执行"powercfg -a"命令,这里会提示当

Ubuntu server版上使用命令行操作VPN客户端

Ubuntu server版上使用命令行操作VPN客户端 VPN,虚拟专用网络,这个技术还是非常实用的.最近笔者参与的项目中就使用上了VPN,大概情况是这样的,有两个开发团队,在异地,代码服务器在深圳了,它使用的是企业内部局域网,支持上网功能的,我们在公网上弄了个阿里云服务器,装了ubuntu vpn的server,其实我们现在要做的就是把代码服务器通过VPN拨号上去,组建一个新的本地网络,怎么大家VPN的server笔者就不在这描述了,网上已经有大把资料了,笔者下面就简要介绍一下自己在代码服务

ubuntu,从新建一个用户,到转到新建用户的命令行操作

题目链接: http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9131   Accepted: 3073 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.