索引能提高檢索數(shù)據(jù)的速度,你可以想像成在MySQL中創(chuàng)建索引一樣,同樣索引也是用B-Tree也實現(xiàn)的。創(chuàng)建schedule的collection: // 實例化Mongo對象,連接27017端口 Mongo mongo = new Mongo("localhost", 27017); // 連接名為yourdb的數(shù)據(jù)庫,假如數(shù)據(jù)庫不存在的話,mongodb會自動建立 DB db = mongo.getDB("test"); //得到要查詢數(shù)據(jù)的 Collction, 如果沒有就創(chuàng)建 DBCollection coll = db.getCollection("schedule");
|
1 | coll.createIndex( new BasicDBObject( "i" , 1 )); // create index on "i", ascending |
查詢collection全部索引
You can get a list of the indexes on a collection :
1 | List<DBObject> list = coll.getIndexInfo(); |
2 |
3 |
4 |
5 | for (DBObject o : list) { |
6 |
7 | System.out.println(o); |
8 |
9 | } |
and you should see something like
打印如下
1 | { "name" : "i_1" , "ns" : "mydb.testCollection" , "key" : { "i" : 1 } } |
|