mongodb 3.0 索引和集合日常操作
阿新 • • 發佈:2019-02-05
-----1.新建索引 db.tblorders.dropIndexes(); db.tblorders.createIndex( { "OrderNo" : 1 },{background:true, name:"index_OrderNo"} ); db.hisorders.createIndex( { "orderno" : 1 },{background:true, name:"hisorders_index_orderno"} ); --ensureIndex已經降級 db.tblorders.ensureIndex({"OrderNo" : 1},{background:true, name:"index_OrderNo"}); db.orderlog.ensureIndex({"OrderNo":1},{background:true, name:"index_orderlog"}); -----2.查詢已有索引 語法: > db.tblorders.getIndexes() db.hisorders.getIndexes() db.tblorders.find({OrderNo:"A2022823590918802334"}).forEach(printjson); db.tblorders.find({OrderNo:"20130901010019880500"}).forEach(printjson); db.tblorders.find({OrderNo:"A2013090106022059954"}).forEach(printjson); db.tblorders.find({OrderNo:"H2013090103019884364"}).explain("executionStats"); db.tblorders.find({OrderNo:"H2013090103019884364"}).explain("executionStats"); -----3. 查詢索引大小 語法: > db.tblorders.totalIndexSize()e ----4.物件改名 use admin; db.runCommand( { renameCollection:"jinrilog.orderlog201508", to: "jinrilog.orderlog", dropTarget: false } ); -----5.刪除索引 db.tblorders.dropIndexes(); -----6.刪除物件 --方法1: > db.tblorders.drop() ; true > db.tblorders.find().count(); 0 > show tables; hisorders t1 > ---方法2: db.tblorders.remove( { } ); db.collection.drop() #是用來從資料庫中刪除一個數據集合包括索引 db.collection.remove() #可以從一個集合中刪除所有文件,刪除匹配一個條件的所有文件,或者限制操作只刪除一條文件,逐條刪除. ---7.kill session > db.currentOp() { "inprog" : [ { "desc" : "conn189", "threadId" : "0x3e083380", "connectionId" : 189, "opid" : 953531, --程序ID "active" : true, "secs_running" : 852, "microsecs_running" : NumberLong(852352681), "op" : "remove", "ns" : "jinri.tblorders", "query" : { }, "client" : "192.168.0.180:46773", "numYields" : 148618, "locks" : { "Global" : "w", "Database" : "w", "Collection" : "w" }, "waitingForLock" : false, "lockStats" : { "Global" : { "acquireCount" : { "r" : NumberLong(148619), "w" : NumberLong(148619) } }, "Database" : { "acquireCount" : { "w" : NumberLong(148619) }, "acquireWaitCount" : { "w" : NumberLong(3) }, "timeAcquiringMicros" : { "w" : NumberLong(388) } }, "Collection" : { "acquireCount" : { "w" : NumberLong(148619) } } } } ] } > db.killOP(953531) { "info" : "attempting to kill op" }