Introduction
Capped collections keep data in a fixed size. They automatically remove old data when full, like a rolling log.
db.createCollection(name, { capped: true, size: maxSizeInBytes, max: maxDocuments })db.createCollection('logs', { capped: true, size: 100000 })
db.createCollection('messages', { capped: true, size: 50000, max: 1000 })
db.createCollection('recentEvents', { capped: true, size: 10240, max: 5 }) // Insert 6 documents for (let i = 1; i <= 6; i++) { db.recentEvents.insertOne({ eventNumber: i, description: `Event ${i}` }) } // Retrieve all documents printjson(db.recentEvents.find().toArray())
capped collection in MongoDB?logs with a maximum size of 1MB?db.createCollection with options including capped: true and size in bytes.capped: true. db.createCappedCollection('logs', 1048576) is invalid.cache with size 100 bytes, what will be the result of inserting 3 documents each of size 50 bytes?db.createCollection('events', { capped: true }) but got an error. What is the likely cause?size option (max bytes) is mandatory.size causes an error. Collection name and method are valid. max is optional.size option specifying the maximum bytes -> Option BuserLogs with a max of 1000 documents and size limit 5MB. Which command correctly creates this collection?capped: true, size in bytes, and max documents.Pass the test and this pattern moves unknown