启动mongodb(默认会连到test)
cangxian@cangxian-CN17S:~$ mongo
MongoDB shell version: 2.4.9
connecting to: test
查看所有的库
> show dbs;
db (empty)
ldblog 0.203125GB
local 0.078125GB
test 0.203125GB
查看当前库
> db;
test
调用库(如果不存在则会创建)
> use newTestDB;
switched to db newTestDB
创建集合(同sql表)
> db.createCollection("testCollection");
{ "ok" : 1 }
查看所有的集合
> show tables;
system.indexes
testCollection
向集合中插入测试数据(同样如果这个集合不存在会创建新的集合在插入数据)
> db.testCollection.insert({"name" : "testCollection"});
查询集合
> db.testCollection.find();
{ "_id" : ObjectId("5a2d3b8e01418fe4d753f463"), "name" : "testCollection" }
删除集合
> db.createCollection("testDeleteCollection");
{ "ok" : 1 }
> db.testDeleteCollection.drop();
true
删除表
> use testDeleteDB;
switched to db testDeleteDB
> db.dropDatabase();
{ "dropped" : "testDeleteDB", "ok" : 1 }
> show tables;
> show dbs;
db (empty)
ldblog 0.203125GB
local 0.078125GB
newTestDB 0.203125GB
test 0.203125GB
testDeleteDB (empty)