

通过使用--eval参数直接执行ti的集合中的数
$./mongo.exe --eval "printjson(db.t1.count())" MongoDB shell version: 2.0.2 connecting to: test 5
2.使用js文件执行文件内容
$cat t1_count.js
var count = db.t1.count();
printjson('count of t1 is: '+count);显示为:
$./mongo t1_count.js MongoDB shell version: 2.0.2 connecting to: test "count of t1 is: 5"
Tips:通过--quiet参数屏蔽部分登陆信息,使结果更清晰
$ ./mongo --quiet t1_count.js "count of t1 is: 5"
> db.currentOp() >db.$cmd.sys.inprog.findOne() //$cmd调用外部函数
显示如下:
> db.currentOp()
{
"inprog" : [
{
"opid" : 630385,
"active" : true,
"lockType" : "read",
"waitingForLock" : false,
"secs_running" : 0,
"op" : "query",
"ns" : "test",
"query" : {
"count" : "t1",
"query" : {
},
"fields" : {
}
},
"client" : "127.0.0.1:51324",
"desc" : "conn",
"threadId" : "0x7f066087f710",
"connectionId" : 7,
"numYields" : 0
}
]
}
>
代码解释:
opid:操作进程号
> db.killOp(630385)
{ "info" : "attempting to kill op" }我们查看下:
> db.currentOp()
{ "inprog" : [ ] }
>使用serverStatus命令可以获取到运行中的MongoDB服务器统计信息,下面我们来执行命令,查看MongoDB服务器的统计信息(不同平台或不同版本的键会有所不同),代码如下:
> db.runCommand({"serverStatus":1})
{
"host" : "lindenpatservermongodb01",
"version" : "2.0.2",
"process" : "mongod",
"uptime" : 6003,
"uptimeEstimate" : 5926,
"localTime" : ISODate("2012-04-15T11:02:21.795Z"),
"globalLock" : {
"totalTime" : 6002811172,
"lockTime" : 24867,
"ratio" : 0.000004142559092311891,
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 0,
"readers" : 0,
"writers" : 0
}
},
"mem" : {
"bits" : 64,
"resident" : 52,
"virtual" : 1175,
"supported" : true,
"mapped" : 160,
"mappedWithJournal" : 320
},
"connections" : {
"current" : 1,
"available" : 818
},
"extra_info" : {
"note" : "fields vary by platform",
"heap_usage_bytes" : 341808,
"page_faults" : 14
},
"indexCounters" : {
"btree" : {
"accesses" : 1,
"hits" : 1,
"misses" : 0,
"resets" : 0,
"missRatio" : 0
}
},
"backgroundFlushing" : {
"flushes" : 100,
"total_ms" : 13,
"average_ms" : 0.13,
"last_ms" : 1,
"last_finished" : ISODate("2012-04-15T11:02:19.010Z")
},
"cursors" : {
"totalOpen" : 0,
"clientCursors_size" : 0,
"timedOut" : 0
},
"network" : {
"bytesIn" : 1729666458,
"bytesOut" : 1349989344,
"numRequests" : 21093517
},
"opcounters" : {
"insert" : 5,
"query" : 8,
"update" : 0,
"delete" : 0,
"getmore" : 0,
"command" : 21093463
},
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 0,
"rollovers" : 0
},
"writeBacksQueued" : false,
"dur" : {
"commits" : 30,
"journaledMB" : 0,
"writeToDataFilesMB" : 0,
"compression" : 0,
"commitsInWriteLock" : 0,
"earlyCommits" : 0,
"timeMs" : {
"dt" : 3073,
"prepLogBuffer" : 0,
"writeToJournal" : 0,
"writeToDataFiles" : 0,
"remapPrivateView" : 0
}
},
"ok" : 1
}
>
使用mongoexport导出数据
先看数据:
> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>
./mongoexport.exe -d test -c t1 -o t1.dat
connected to: 127.0.0.1
exported 5 records
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ls
bsondump mongodump mongoimport mongosniff t1_count.js
mongo mongoexport mongorestore mongostat t1.dat
mongod mongofiles mongos mongotop testfiles.txt
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ cat t1.dat
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
./mongoexport.exe -d test -c t1 -o t1.dat 使用参数说明
-d: 指明使用的数据库导出的数据格式的是JSON方式,也可导出csv格式;
导出为CSV格式代码文件如下:
./mongoexport -d test -c t1 -csv -f num -o t1.dat
数据导入 mongoimport
现将ti删除:
> db.t1.drop() true > show collections system.indexes system.users >
再导入数据,这里导入的是csv数据:
./mongoimport -d test -c t1 --type csv --headerline -file /home/t1.dat connected to: 127.0.0.1
看看导入的数据是不是一样:
> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>
./mongodump -d test -o /home/dump
mongorestore获取mongodump的输出结果,并将备份的数据插入到运行的MongoDB中。
./mongorestore -d test dump/*
connected to: 127.0.0.1
Thu Apr 19 18:16:12 dump/test/system.users.bson
Thu Apr 19 18:16:12 going into namespace [test.system.users]
2 objects found
Thu Apr 19 18:16:12 dump/test/t1.bson
Thu Apr 19 18:16:12 going into namespace [test.t1]
5 objects found
Thu Apr 19 18:16:12 dump/test/system.indexes.bson
Thu Apr 19 18:16:12 going into namespace [test.system.indexes]
Thu Apr 19 18:16:12 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }
Thu Apr 19 18:16:13 { key: { _id: 1 }, ns: "test.t1", name: "_id_" }
2 objects found