当前位置:网站首页>MongoDB命令汇总
MongoDB命令汇总
2022-07-07 11:17:00 【cui_yonghua】
基础篇(能解决工作中80%的问题):
进阶篇:
其它:
一. MongoDB命令帮助系统
看每个命令的详细用法,可以使用:db.listCommands()
数据库操作更详细的帮助命令:db.help()
对指定数据库的集合进行操作、管理和监控:db.mycoll.help()
二. 基本命令及实例
(一)基本命令
显示当前数据库服务器上的数据库:show dbs
切换到指定数据库pagedb的:use pagedb
显示数据库中所有的集合:show collections 或 show tables
查看数据库服务器的状态:db.serverStatus()
查询指定数据库统计信息:db.stats()
(二)常用DDL和DML
了解 DML、DDL、DCL、DQL专业名称
SQL语言共分为四大类:数据操纵语言DML,数据定义语言DDL,数据控制语言DCL,数据查询语言DQL。
DML(data manipulation language 数据操纵语言): 它们是UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言
DDL(data definition language 数据(库)定义语言): 主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用
DCL(Data Control Language 数据控制语言): 是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL
DQL(Data Query Language 数据查询语言):是数据库控制功能,包括 select 语句
创建数据库 mydatabase: use mydatabase; (只是切换,不是创建)
创建集合 studentdb.createCollection("student",{capped:true,size:1000,max:100})
试一下,max等于3或者size等于1,会怎么样?
插入更新记录
表(student)
{
_id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]}
{
_id:2,classid:2,age:19,name:"little2",love:["play"]}
{
_id:3,classid:2,age:20,name:"little3"}
{
_id:4,classid:1,age:21,name:"little4"}
{
_id:5,classid:1,age:22,name:"little5",opt:"woshisheia"}
{
_id:6,classid:2,age:23,name:"23little4"}
插入
db.student.save({
_id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]})
db.student.save({
_id:2,classid:2,age:19,name:"little2",love:["play"]})
db.student.save({
_id:3,classid:2,age:20,name:"little3"})
db.student.save({
_id:4,classid:1,age:21,name:"little4"})
db.student.save({
_id:5,classid:1,age:22,name:"little5",opt:"woshisheia"})
db.student.save({
_id:6,classid:2,age:23,name:"23little4"})
或者
db.student.insert([
{
_id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]},
{
_id:2,classid:2,age:19,name:"little2",love:["play"]},
{
_id:3,classid:2,age:20,name:"little3"},
{
_id:4,classid:1,age:21,name:"little4"},
{
_id:5,classid:1,age:22,name:"little5",opt:"woshisheia"},
{
_id:6,classid:2,age:23,name:"23little4"},
])
总结:集合一旦启动封顶模式,当max或者是size超出,新数据覆盖旧数据
查询一条记录/多条记录
查询出name为little1的数据: db.student.find({name:"little1"})
从第二条查寻,查出三条: db.student.find().skip(1).limit(3)
查询大于19小于等于21:db.student.find({age:{$gt:19,$lte:21}})
查询出age为奇数的数据(对2求余为1即为奇数):db.student.find({age:{$mod:[2,1]}})
查询出存在opt字段的数据:db.student.find({opt:{$exists:true}})
查询出不存在opt字段的数据:db.student.find({opt:{$exists:false}})
查询出name不为little2的数据:db.student.find({name:{$ne:"little2"}})
查询出age为16,18,19的数据:db.student.find({age:{$in:[16,18,19]}})
查询出age不为16,18,19的数据:db.student.find({age:{$nin:[16,18,19]}})
查询出love有三个的数据:db.student.find({love:{$size:3}})
查询出name不是以litt开头的数据:db.student.find({name:{$not:/^litt.*/}})
查询age=“”>20的数据:db.student.find({age:{$gt:20}})
按age升序:db.student.find().sort({age:1})
按age降序:db.student.find().sort({age:-1})
查询数据条数:db.student.find().count()
删除记录
删除age为20的数据:db.student.remove({age:20});
创建索引
创建索引 age:db.student.ensureIndex({"age":1})
查询索引:db.student.getIndexes()
删除索引:db.student.dropIndex({"age" : 1})
统计集合记录数:db.student.count()
删除集合 student:db.student.drop()
删除数据库 mydatabase:db.dropDatabase()
(三)安全管理
以安全认证模式启动
添加用户
安全认证
(四)数据备份、恢复与迁移管理
备份全部数据库
备份指定数据库
备份一个数据库中的某个集合
恢复全部数据库
恢复某个数据库的数据
恢复某个数据库的某个集合的数据
向MongoDB导入数据
从MongoDB导出数据
(五)远程连接管理
基于mongo实现远程连接mongo —host 192.168.17.129 —port 27017
(六)聚合
计算各班级平均年龄
db.student.aggregate([
{
$group: {
_id: "$classid",
avg_age: {
$avg: "$age"
}
}
}
])
计算全班级平均年龄
db.student.aggregate([
{
$group: {
_id: null,
avg_age: {
$sum: "$age"
}
}
}
])
(七) MapReduce
计算各班级平均年龄
分组统计(MapReduce):
var mapfun = function(){
emit(this.classid,this.age);}
var reducefun = function(key, values){
var total=0;
for(var i=0;i<values.length;i++){
total += values[i];
}
return {
classid: key, age_avg: total/values.length };
}
db.student.mapReduce(
mapfun,
reducefun,
{
out: "student_res" }
)
查询统计结果(分班级统计人数):db.student_res.find()
各班级统计人数
分组统计(MapReduce):
var mapfun = function(){
emit(this.classid,1);}
var reducefun = function(key, values){
return Array.sum(values);
}
db.student.mapReduce(
mapfun,
reducefun,
{
out: "student_sta" }
)
查询统计结果(分班级统计人数):db.student_sta.find();
边栏推荐
- 自定义线程池拒绝策略
- How to reset Google browser? Google Chrome restore default settings?
- Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
- Unity build error: the name "editorutility" does not exist in the current context
- Four functions of opencv
- leecode3. 无重复字符的最长子串
- 线程池拒绝策略最佳实践
- Enterprise custom form engine solution (XII) -- experience code directory structure
- [difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
- Sequoia China completed the new phase of $9billion fund raising
猜你喜欢

Analysis of DHCP dynamic host setting protocol

Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]

高瓴投的澳斯康生物冲刺科创板:年营收4.5亿 丢掉与康希诺合作

About how appium closes apps (resolved)

Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images

Sequoia China completed the new phase of $9billion fund raising

Blog recommendation | Apache pulsar cross regional replication scheme selection practice

About the problem of APP flash back after appium starts the app - (solved)

COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~

Go语言学习笔记-结构体(Struct)
随机推荐
初学XML
Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
【无标题】
线程池拒绝策略最佳实践
HZOJ #235. Recursive implementation of exponential enumeration
Awk of three swordsmen in text processing
What kind of methods or functions can you view the laravel version of a project?
MATLAB中polarscatter函数使用
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
《ASP.NET Core 6框架揭秘》样章[200页/5章]
[binary tree] delete points to form a forest
关于 appium 启动 app 后闪退的问题 - (已解决)
- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net
Milkdown 控件图标
error LNK2019: 无法解析的外部符号
HZOJ #240. Graphic printing IV
2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation
Cookie
飞桨EasyDL实操范例:工业零件划痕自动识别