MongoDB常用数据库命令第一集
1、查询操作
(1)Help查看命令提示
help
db.help()
db.test.help()
db.test.find().help()
(2)创建/切换数据库
use music
(3)查询数据库
show dbs
(4)查看当前使用的数据库
db/db.getName()
(5)显示当前DB状态
db.stats()
(6)查看当前DB版本
db.version()
(7)查看当前DB的链接机器地址
db.getMongo()
(8)删除数据库
db.dropDatabase()
2、集合操作:
(1)创建一个聚集集合
db.createCollection("collName", {size: 20, capped: true, max: 100});
db.collName.isCapped(); //判断集合是否为定容量
(2)得到指定名称的聚集集合
db.getCollection("account");
(3)得到当前db的所有聚集集合
db.getCollectionNames();
(4)显示当前db所有聚集的状态
db.printCollectionStats();
3、添加、修改与删除集合数据:
(1)添加
db.users.save({name: ‘zhangsan', age: 25, sex: true});
(2)修改
db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
相当于:update users set name = ‘changeName' where age = 25;
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
相当于:update users set age = age + 50 where name = ‘Lisi';
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);
相当于:update users set age = age + 50, name = ‘hoho' where name = ‘Lisi';
(3)删除
db.users.remove({age: 132});
(4)查询修改删除
db.users.findAndModify({
query: {age: {$gte: 25}},
sort: {age: -1},
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
});
db.runCommand({ findandmodify : "users",
query: {age: {$gte: 25}},
sort: {age: -1},
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
});
4、集合查询:
(1)查询所有记录
db.userInfo.find();
相当于:select* from userInfo;
(2)查询去重后数据
db.userInfo.distinct("name");
相当于:select distict name from userInfo;
(3)查询age = 22的记录
db.userInfo.find({"age": 22});
相当于: select * from userInfo where age = 22;
(4)查询age > 22的记录
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;
(5)查询age < 22的记录
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;
(6)查询age >= 25的记录
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;
(7)查询age <= 25的记录
db.userInfo.find({age: {$lte: 25}});
(8)查询age >= 23 并且 age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});
(9)查询name中包含 mongo的数据
db.userInfo.find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%’;
(10)查询name中以mongo开头的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;
(11)查询指定列name、age数据
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
(12)查询指定列name、age数据, age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;
(13)按照年龄排序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});
(14)查询name = zhangsan, age = 22的数据
db.userInfo.find({name: 'zhangsan', age: 22});
相当于:select * from userInfo where name = ‘zhangsan' and age = ’22';
(15)查询前5条数据
db.userInfo.find().limit(5);
相当于:select top 5 * from userInfo;
MongoDB常用数据库命令第一集的更多相关文章
- MongoDB常用数据库命令第二集
=======================基础命令======================= mongo 进入数据库操作界面db 查看当前使用的数据库show dbs 查看当前已经被创建出来的 ...
- MongoDB常用数据库命令第三集
show dbs 查看已经存在的数据库 use 数据库名 切换到指定的数据库(无论数据库是否存在 均可切换成功) db 查看当前数据库 db.getCollectionNames() 查看当前数据库下 ...
- MongoDB之数据库命令操作(二)
现在详细学习一下mongodb的数据库操作. 查询语句 db.xxx(集合name).find() # 查询 db.xxx(集合name).findOne() # 只返回一个 db.xxx(集合nam ...
- postgresql 常用数据库命令
连接数据库, 默认的用户和数据库是postgrespsql -U user -d dbname 切换数据库,相当于MySQL的use dbname\c dbname列举数据库,相当于mysql的sho ...
- mongodb数据库命令
常用数据库命令汇总 Database Commands Api 下面简单列一下Shell常用的基本命令 启动连接Mongodb #带配置信息启动 mongod -f xxx.conf #连接 mong ...
- 【Mongodb教程 第十七课 】MongoDB常用命令 数据库命令 集合操作命令
(1)数据库命令 a)添加用户 db.addUser(‘name’,’pwd’) b)删除用户 db.removeUser(‘name’) c)用户验证 db.auth(‘name’,’pwd’) d ...
- 每篇半小时1天入门MongoDB——4.MongoDB索引介绍及数据库命令操作
准备工作 继续连接到mongo C:\Users\zouqi>mongo MongoDB shell version: 3.0.7 connecting to: test 查看数据库和集合 &g ...
- MongoDB 数据库命令
数据库命令 连接成功后,默认使用test数据库 查看当前数据库名称 db 查看所有数据库名称,列出所有在物理上存在的数据库 show dbs 切换数据库,如果数据库不存在也并不创建,直到插入数据或创建 ...
- MongoDB学习笔记-06 数据库命令、固定集合、GridFS、javascript脚本
介绍MongoDB支持的一些高级功能: 数据库命令 固定大小的集合 GridFS存储大文件 MongoDB对服务端JavaScript的支持 数据库命令 命令的原理 MongoDB中的命令其实是作为一 ...
随机推荐
- 第02节-BLE协议各层的形象化理解
本篇博客根据韦大仙视频,整理所得. 先上框图: ATT层 从ATT开始看,在上篇博客讲的医院结构里面有个检验室,检验室可以得到各项结果,但是它并不知道这些结果代表什么含义.类比的在BLE协议栈里面,A ...
- 201871010128-杨丽霞《面向对象程序设计(java)》第二周学习总结
201871010128-杨丽霞<面向对象程序设计(java)>第二周学习总结 项目 内容 这个作业属于哪个课程 <https://www.cnblogs.com/nwnu-daiz ...
- 查询接口---flask+python+mysql
环境准备 安装flask pip install flask 项目结构如图 1.新建配置文件conf.py #!/usr/bin/python# -*- coding:utf-8 -*- impor ...
- CentOS7.5 上使用 bundle 文件安装 MySQL8.0 MySQL5.0
CentOS7.5 上使用 bundle 文件安装 MySQL8.0 MySQL5.0 CentOS7.5 环境 [root@instance-fjii60o3 ~]# rpm -qi centos- ...
- Python基础之内置方法
目录 字符串的内置方法 按索引取值 切片(顾头不顾尾,步长) 长度len 成员运算 移除两边空白strip 切分split 循环 lower&upper startswith & en ...
- 防御流类型的xss攻击
1.建立一个工具类 package im.lsn.oss.exhibition.utils; import org.apache.commons.lang3.StringUtils; import j ...
- Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
链接: https://codeforces.com/contest/1278/problem/C 题意: Karlsson has recently discovered a huge stock ...
- IDEA Gradle配置与使用
1.安装Gradle,并添加环境变量. https://www.cnblogs.com/NyanKoSenSei/p/11458953.html 2.在IDEA中设置Gradle: 3.选中项目中的. ...
- System.getProperty("line.separator") 是什么意思?
在java中存在一些转义字符,比如"\n"为换行符,但是也有一些JDK自带的一些操作符 比如 : System.getProperty("line.separator&q ...
- button 在chrome浏览器下被点击时会出现一个橙色的边框
问题描述: button在chrome浏览器下被点击时会出现一个橙色的边框 在button上添加 :focus{outline:none;}也无法解决. 解决办法: 在button上添加 :focus ...