日期:2014-05-16 浏览次数:20551 次
Mongo m = null; try { //连接数据库(三种连接方式): m = new Mongo("127.0.0.1", 25017); //选择数据库,如果没有这个数据库的话,会自动建立 DB db = m.getDB("dnt_mongodb"); //列出所有数据库名,不过发现,如果数据库里面是没有数据的话,并不会被列出来 System.out.println("数据库列表:\n"+m.getDatabaseNames()); DBCollection collection = db.getCollection("posts1"); System.out.println("统计文档:" + collection.count()); BasicDBObject document = new BasicDBObject(); document.put("id", 1); collection.createIndex(document); document = new BasicDBObject(); document.put("url", 1); collection.createIndex(document); for(int i = 1; i <= 10000000; i++){ System.out.println(i); document = new BasicDBObject(); document.put("id", i); document.put("url", "http://www.agrilink.cn/NewNongSou/GNZX/index2011.html?OK=" + i); //将新建立的document保存到collection中去 collection.insert(document); } System.out.println("Done"); } catch(Exception error) { error.printStackTrace(); } finally { m.close(); }
mongos> use dnt_mongodb switched to db dnt_mongodb mongos> db.posts1.stats() { "sharded" : true, "flags" : 1, "ns" : "dnt_mongodb.posts1", "count" : 8963724, "numExtents" : 65, "size" : 932223312, "storageSize" : 1175728128, "totalIndexSize" : 1595456464, "indexSizes" : { "_id_" : 290926608, "id_1" : 250226480, "url_1" : 1054303376 }, "avgObjSize" : 103.99955554187076, "nindexes" : 3, "nchunks" : 42, "shards" : { "shard0000" : { "ns" : "dnt_mongodb.posts1", "count" : 1861097, "size" : 193554088, "avgObjSize" : 104, "storageSize" : 272621568, "numExtents" : 16, "nindexes" : 3, "lastExtentSize" : 51974144, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 398048560, "indexSizes" : { "_id_" : 60396112, "id_1" : 51950304, "url_1" : 285702144 }, "ok" : 1 }, "shard0001" : { "ns" : "dnt_mongodb.posts1", "count" : 1857613, "size" : 193191752, "avgObjSize" : 104, "storageSize" : 272621568, "numExtents" : 16, "nindexes" : 3, "lastExtentSize" : 51974144, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 282807840, "indexSizes" : { "_id_" : 60281648, "id_1" : 51852192, "url_1" : 170674000 }, "ok" : 1 }, "shard0002" : { "ns" : "dnt_mongodb.posts1", "count" : 1829738, "si