Java tutorial
/* * Copyright 2014 David Salter <david@developinjava.com>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.davidsalter.cappedcollection; import com.mongodb.DB; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import java.net.UnknownHostException; import org.mongodb.morphia.Datastore; import org.mongodb.morphia.Morphia; /** * Class illustrating how to create a capped collection in MongoDB using Morphia. * @author <a href="mailto:david@developinjava.com">David Salter</a> */ public class MorphiaCappedCollection { /** * @param args the command line arguments */ public static void main(String[] args) throws UnknownHostException { MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost")); DB db = mongoClient.getDB("test"); Morphia morphia = new Morphia(); morphia.map(LogEntry.class); Datastore datastore = morphia.createDatastore(mongoClient, "test"); datastore.ensureCaps(); for (int i = 0; i < 8; i++) { LogEntry logEntry = new LogEntry(i); datastore.save(logEntry); } } }