mongodb 3.0 索引和集合日常操作

时间:2022-06-01 22:16:10
-----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" }