Use the DB.createCollection operation, and then specify DBObject with capped as the parameter. You can then specify the size and max to control the byte size and the maximum number of documents. The MongoDB website has a tutorial on private collections that explains all the options, but there is no example for each driver.
Mongo mongo = new Mongo("127.0.0.1"); DB db = mongo.getDB("mydbid"); DBCollection collection; if (db.collectionExists("mycollection")) { collection = db.getCollection("mycollection"); } else { DBObject options = BasicDBObjectBuilder.start().add("capped", true).add("size", 2000000000l).get(); collection = db.createCollection("mycollection", options); } }
Lee jensen
source share