博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb 命令
阅读量:4069 次
发布时间:2019-05-25

本文共 1702 字,大约阅读时间需要 5 分钟。

数据库管理:

show dbs:显示数据库列表

show collections:显示当前数据库中表的集合(类似于关系数据中的表)

show users:显示当前数据库用户

use <dbname>:切换数据库

db.help()  :数据库管理命令

db.foo.help():显示集合操作命令

db.dropDatabase():删除当前使用的数据库

db.getName():显示当前使用的数据库

db.stats():显示当前数据库的状态

db.version():显示数据库版本

db.getMongo():查看当前数据库的ip地址

用户管理:

添加用户:db.addUser("root","password");

数据库认证:db.auth("root","password")

密码修改:db.addUser("root","pass",true) 最后一个参数readOnly,读写权限设置

集合管理:

查询:

1、创建集合:

db.createCollections('user', {  max: 100,  size: 10000,  capped: true})

2、插入记录

db.user.insert({  name: "tom",  age: 26,  status: "A",  groups: ["news", "sports"]});

3、查询全部

db.user.find()

4、limt(5)查询前5条:

db.user.find({  age: {    $gt: 30  }}, {  name: 1,  groups: 1}).limit(5);相当于select id,name,groups form userwhere age>30limit(5)

5、skip(10)查询10条以后的记录

db.user.find().skip(10);相当于:select * from user where id not in(select top 10 from user)

6、sort(age)按照年龄排序

升序:db.user.find().sort(age:1)降序:db.user.find().sort(age:-1)

7、查询在5-10之间的数据

db.user.find().limit(10).skip(5);可用于分页,limit是pageSize,skip是第几页*pageSize

8、查询去掉后的当前聚集集合中的某列的重复数据

db.user.distinct("name");会过滤掉name中的相同数据相当于:select distict name from usero;
9、查询age = 22的记录
db.user.find({"age": 22});
相当于: select * from user where age = 22;
 
10、查询age > 22的记录
db.user.find({age: {$gt: 22}});
相当于:select * from user where age >22;
 
11、查询age < 22的记录
db.user.find({age: {$lt: 22}});
相当于:select * from user where age <22;
 
12、查询age >= 25的记录
db.user.find({age: {$gte: 25}});
相当于:select * from user where age >= 25;
 
13、查询age <= 25的记录
db.user.find({age: {$lte: 25}});
 
14、查询age >= 23 并且 age <= 26
db.user.find({age: {$gte: 23, $lte: 26}});
 
15、查询name中包含 mongo的数据
db.user.find({name: /mongo/});
//相当于%%
select * from user where name like ‘%mongo%’;
 

转载地址:http://dirji.baihongyu.com/

你可能感兴趣的文章
S3C2440中对LED驱动电路的理解
查看>>
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
js判断空对象的几种方法
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>