27
loading...
This website collects cookies to deliver better user experience
“Capped collections are fixed-size collections that support high-throughput operations that insert, retrieve, and delete documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.”
db.createCollection
command:db.createCollection("logs", {capped: true,
size: 4096,
max:5})
db.createCollection
command, this time passing a BasicDBObject
as the parameter. This parameter has the fields “capped”, “size” and “max” which specify that the collection is capped, the maximum size of the collection in bytes and the maximum number of entries in the collection. The following code snippet shows how to connect to a local instance of MongoDB and create a capped collection.MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
DB db = mongoClient.getDB("test");
DBCollection collection;
if (!db.collectionExists("cappedLogsJavaDriver")) {
BasicDBObject options = new BasicDBObject("capped", true);
options.append("size", 4096);
options.append("max", 5);
collection = db.createCollection("cappedLogsJavaDriver", options);
} else {
collection = db.getCollection("cappedLogsJavaDriver");
}
for (int i = 0; i < 8; i++) {
BasicDBObject logEntry = new BasicDBObject("logId", i);
collection.insert(logEntry);
}
> db.cappedLogsJavaDriver.find()
{ "_id" : ObjectId("54a1ca44a82617da4f72e025"), "logId" : 3 }
{ "_id" : ObjectId("54a1ca44a82617da4f72e026"), "logId" : 4 }
{ "_id" : ObjectId("54a1ca44a82617da4f72e027"), "logId" : 5 }
{ "_id" : ObjectId("54a1ca44a82617da4f72e028"), "logId" : 6 }
{ "_id" : ObjectId("54a1ca44a82617da4f72e029"), "logId" : 7 }
@Entity
annotation, which are then stored within a collection usually named after the class being annotated. To create a capped collection, we must add additional values onto the @Entity
annotation to specify the maximum number of entries in the collection and the size of the collection. Modelling the same type of object as used in the example for the Java Driver, we would create a LogEntry class as follows:@Entity(value="cappedLogsMorphia", cap=@CappedAt(count=5, value=4096))
public class LogEntry {
private int logId;
@Id
private ObjectId id;
public LogEntry(int logId) {
this.logId = logId;
}
public int getLogId() {
return logId;
}
public void setLogId(int logId) {
this.logId = logId;
}
}
@Entity
specifying that the collection should be capped with a maximum of 5 documents and a size of 4096 bytes..ensureCaps()
method on the Morphia Datastore
as shown below.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);
}
> db.cappedLogsMorphia.find()
{ "_id" : ObjectId("54a1ce9da82629642c64f5d9"), "className" : "com.cloudblogaas.cappedcollection.LogEntry", "logId" : 3 }
{ "_id" : ObjectId("54a1ce9da82629642c64f5da"), "className" : "com.cloudblogaas.cappedcollection.LogEntry", "logId" : 4 }
{ "_id" : ObjectId("54a1ce9da82629642c64f5db"), "className" : "com.cloudblogaas.cappedcollection.LogEntry", "logId" : 5 }
{ "_id" : ObjectId("54a1ce9da82629642c64f5dc"), "className" : "com.cloudblogaas.cappedcollection.LogEntry", "logId" : 6 }
{ "_id" : ObjectId("54a1ce9da82629642c64f5dd"), "className" : "com.cloudblogaas.cappedcollection.LogEntry", "logId" : 7 }
.stats()
method on the collection from within the Mongo DB interactive shell.> db.cappedLogsJavaDriver.stats()
{
"ns" : "test.cappedLogsJavaDriver",
"count" : 5,
"size" : 180,
"avgObjSize" : 36,
"storageSize" : 4096,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 4096,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"capped" : true,
"max" : 5,
"ok" : 1
}