mongodb常见管理命令(一)

2015-11-21 02:07:54 · 作者: · 浏览: 45
----------1.复制数据库

wind:PRIMARY> show dbs;
jinri  0.078GB
local  1.078GB
test   0.078GB
wind   0.078GB

wind:PRIMARY> use admin;
switched to db admin


db.runCommand(
{copydb : 1,
fromhost : "localhost",
fromdb : "wind",
todb : "snow"
}
);

#执行结果
wind:PRIMARY> use admin;
switched to db admin

wind:PRIMARY> db.runCommand(
... {copydb : 1,
... fromhost : "localhost",
... fromdb : "wind",
... todb : "snow"
... }
... );
{ "ok" : 1 }
wind:PRIMARY> show dbs;
jinri  0.078GB
local  1.078GB
snow   0.078GB
test   0.078GB
wind   0.078GB
wind:PRIMARY> use snow;
switched to db snow
wind:PRIMARY> show tables;
system.indexes
tblorders


wind:PRIMARY> use snow;
switched to db snow
wind:PRIMARY> db.tblorders.find().count();
3


-------2.重命名集合

use admin;

db.runCommand(
{
renameCollection:"snow.tblorders",
to: "wind.tblbooks",
dropTarget: false
}
);


--case01:

wind:PRIMARY> use snow;
switched to db snow
wind:PRIMARY> show tables;
system.indexes
tblorders

wind:PRIMARY> use wind;
switched to db wind
wind:PRIMARY> show tables;
system.indexes
tblorders


wind:PRIMARY> use admin;
switched to db admin

wind:PRIMARY> 
wind:PRIMARY> db.runCommand(
... {
... renameCollection:"snow.tblorders",
... to: "wind.tblbooks",
... dropTarget: false
... }
... );
{ "ok" : 1 }
wind:PRIMARY> use snow;
switched to db snow
wind:PRIMARY> show tables;
system.indexes
wind:PRIMARY> use wind;
switched to db wind
wind:PRIMARY> show tables;
system.indexes
tblbooks
tblorders


--case02:


wind:PRIMARY> use wind;
switched to db wind
wind:PRIMARY>
show tables; system.indexes tblbooks tblorders wind:PRIMARY> use snow; switched to db snow wind:PRIMARY> show tables; system.indexes use admin; db.runCommand( { renameCollection:"wind.tblorders", to: "snow.tblbooks", dropTarget: true } ); wind:PRIMARY> use wind; switched to db wind wind:PRIMARY> show tables; system.indexes tblbooks wind:PRIMARY> use snow; switched to db snow wind:PRIMARY> show tables; system.indexes tblbooks wind:PRIMARY> use snow; switched to db snow wind:PRIMARY> show tables; system.indexes tblbooks -------3.查看数据库和集合的统计信息 db.stats(); wind:PRIMARY> db.tblorders.stats(); { "ns" : "jinri.tblorders", "count" : 5, "size" : 560, "avgObjSize" : 112, "numExtents" : 1, "storageSize" : 8192, "lastExtentSize" : 8192, "paddingFactor" : 1, "paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.", "userFlags" : 1, "capped" : false, "nindexes" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1 } wind:PRIMARY> db.stats(); { "db" : "jinri", #数据库名 "collections" : 3, #collection的数量 "objects" : 9, #对象数据量 "avgObjSize" : 90.66666666666667, #对象平均大小 "dataSize" : 816, #数据大小 "storageSize" : 20480, #数据存储大小包括预分配空间 "numExtents" : 3, #事件数量 "indexes" : 1, #索引数量 "indexSize" : 8176, #索引大小 "fileSize" : 67108864, #文件大小 "nsSizeMB" : 16, "extentFreeList" : { "num" : 0, "totalSize" : 0 }, "dataFileVersion" : { "major" : 4, "minor" : 22 }, "ok" : 1 #本次stats是否正常 } ---4.检查数据库 wind:PRIMA