mongoDB的安装与使用

1 安装

1
2
3
4
5
// 先更新brew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
// 安装mongodb
$ brew install mongodb

等待安装成功

1
2
3
4
To have launchd start mongodb now and restart at login:
brew services start mongodb
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf

根据提示启动服务

1
$ brew services start mongodb

2 使用

1
2
3
4
5
6
$ mongo
> db.test.insert({name: 'lxc'})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("58d0c768a060bd0547d979a3"), "name" : "lxc" }
>

创建数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> show dbs
admin 0.000GB
local 0.000GB
> use test
switched to db test
// 这时候并不会显示test,要插入一条数据才可以
> show dbs
admin 0.000GB
local 0.000GB
> db.test.insert({name: 'lxc'})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
>

删除数据库

1
2
3
4
5
6
7
8
> db
test
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
local 0.000GB
>