【MongoDB学习笔记4】MongoDB管理客户端和创建数据库

简介:

MongoDB管理客户端背后是一个JavaScript Shell,是一个完整的JavaScript解释器,用”mongo”命令登入;

进入后进入默认的test数据库,可以用db命令查看当前的所连接的数据库:

1
2
3
4
5
6
[root@localhost ~] # mongo   
MongoDB shell version: 2.6.6    
connecting to:  test    
> db    
test    
>

 输入help来查找可用的命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> help           
db.help()                    help on db methods            
db.mycoll.help()             help on collection methods            
sh.help()                    harding helpers            
rs.help()                    replica  set  helpers            
help admin                   administrative help            
help connect                 connecting to a db help            
help keys                    key shortcuts            
help misc                    misc things to know            
help mr                      mapreduce
show dbs                     show database names           
show collections             show collections  in  current database            
show  users                    show  users  in  current database            
show profile                 show most recent system.profile entries with  time  >= 1ms 
show logs                    show the accessible logger names            
show log [name]              prints out the last segment of log  in  memory,  'global'  is default            
use <db_name>                 set  current database            
db.foo. find ()                list objects  in  collection foo            
db.foo. find ( { a : 1 } )     list objects  in  foo where a == 1            
it                           result of the last line evaluated; use to further iterate   
 
DBQuery.shellBatchSize = x    set  default number of items to display on shell           
exit                          quit the mongo shell    
>

 

MongoDB没有显示的创建数据库的命令和删除的数据库的命令,创建数据库用use即可;

创建blog的数据库:   

1
2
> use blog    
switched to db blog

   
插入一个集合和文档:

1
2
> db.post.insert({ "geeting" :3});  
WriteResult({  "nInserted"  : 1 })

查看创建的数据库和集合:

1
2
3
4
5
6
7
> show dbs  
admin  (empty)  
blog   0.078GB  
local   0.078GB  
> show collections  
post  
system.indexes

删除数据库

1
2
3
4
5
> db.dropDatabase();   
{  "dropped"  :  "blog" ,  "ok"  : 1 }    
> show dbs    
admin  (empty)    
local   0.078GB




本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1598286,如需转载请自行联系原作者

相关文章
|
NoSQL IDE MongoDB
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
770 3
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
1540 79
|
NoSQL IDE MongoDB
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
753 1
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
NoSQL IDE MongoDB
Studio 3T 2025.18 发布 - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.18 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
780 0
Studio 3T 2025.18 发布 - MongoDB 的终极 GUI、IDE 和 客户端
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
601 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
|
NoSQL IDE MongoDB
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
1735 21
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
NoSQL IDE MongoDB
Studio 3T 2025.7 发布 - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.7 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
391 3
Studio 3T 2025.7 发布 - MongoDB 的终极 GUI、IDE 和 客户端
|
运维 NoSQL 容灾
告别运维噩梦:手把手教你将自建 MongoDB 平滑迁移至云数据库
程序员为何逃离自建MongoDB?扩容困难、运维复杂、高可用性差成痛点。阿里云MongoDB提供分钟级扩容、自动诊断与高可用保障,助力企业高效运维、降本增效,实现数据库“无感运维”。
|
NoSQL IDE MongoDB
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
691 2
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端

推荐镜像

更多