MongoDB 入门专栏
http://blog.csdn.net/column/details/19681.html
数据库操作
查看数据库
# 查看当前mongo下的所有数据库
> show databases
> show dbs
创建数据库
# 连接,创建 testdb 数据库
> use testdb
删除数据库
# 删除 testdb 数据库
> use testdb
> db.dropDatabase()
复制数据库
# 复制 testdb 库为 testdb_copy 库
> use testdb
> db.copyDatabase("testdb", "testdb_copy")
重命名数据库
# 重命名 testdb 为 testdb233
> use testdb
> db.copyDatabase('testdb', 'testdb233')
> db.dropDatabase()
# 重命名 testdb 为 testdb233
> use testdb
> db.runCommand( { renameCollection:'testdb.orders', to: 'testdb233.orders' } )
集合操作
查看集合
# 显示指定数据库的所有集合
> use testdb
> show collections # 或者 show tables
创建集合
- autoIndexID:是否在 _id 字段自动创建索引,默认为 false;
- capped:是否创建固定集合,指定为 true 时当集合容量到最大值时,会自动覆盖最早的文档,当指定 capped时,必须指定 size 参数;
- size:指定固定集合的大小的上限,单位为字节;
- max:指定固定集合的文档数量上限;
> use testdb
> db.createCollection('articles')
> db.createCollection( 'site', { capped:true, autoIndexID:true, size:233300, max: 10000} )
删除集合
# 删除 testdb 的 site 集合
> use testdb
> db.site.drop()
重命名集合
# 重命名 testdb 库的 site 集合为 site233
> use testdb
> db.site.renameCollection('site233')