索引能提高检索数据的速度,你可以想像成在MySQL中创建索引一样,同样索引也是用B-Tree也实现的。创建schedule的collection: // 实例化Mongo对象,连接27017端口 Mongo mongo = new Mongo("localhost", 27017); // 连接名为yourdb的数据库,假如数据库不存在的话,mongodb会自动建立 DB db = mongo.getDB("test"); //得到要查询数据的 Collction, 如果没有就创建 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 } } |
|
来自: CevenCheng > 《Java Client》