|
MongoDB管理客户端背后是一个JavaScript Shell,是一个完整的JavaScript解释器,用”mongo”命令登入;
进入后进入默认的test数据库,可以用db命令查看当前的所连接的数据库:
1
2
3
4
5
6
| [iyunv@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
|
|
|