[SERVER-4542] Option "size" for capped collections Created: 21/Dec/11  Updated: 29/Feb/12  Resolved: 23/Dec/11

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Uladzimir Mihura Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None

Issue Links:
Related
Participants:

 Description   

In docs:

Unlike a standard collection, you must explicitly create a capped collection, specifying a collection size in bytes. The collection's data space is then preallocated. Note that the size specified includes database headers.

But if we take a look at the following code, the insertion of document that larger 10 bytes is possible:

Mongo m = new Mongo();
DB db = m.getDB("test");
db.createCollection("cn", BasicDBObjectBuilder.start()
        .add("size", 10)
        .add("capped", true)
        .get());
DBCollection collection = db.getCollection("cn");
collection.insert(BasicDBObjectBuilder.start()
        .add("field", "datadatadatadatadatadatadatadata")
        .add("maxLong", Long.MAX_VALUE)
        .add("minLong", Long.MIN_VALUE)
        .get()
);
System.out.println(collection.findOne());



 Comments   
Comment by Vitali Ausianik [ 22/Dec/11 ]

Also we can see similar bug with Python

import pymongo
 
args = {"name": "tmpcoll", "options": { "capped": True, "size":5, "max":6} }
db = pymongo.Connection().testdb
db.create_collection(**args)
 
to_ins = []
to_ins.append({"field": "some string for add 1"})
to_ins.append({"field": "some string for add 2"})
to_ins.append({"field": "some string for add 3"})
 
db.tmpcoll.insert(to_ins, safe=True)
 
# Now we can find our three strings in collection "tmpcoll" as we inserted. The size of collection is 168b.
# But we mustn't find them, because collection creates as capped with size 5b

Comment by Vitali Ausianik [ 22/Dec/11 ]

Should we create an additional issue?

Python on Ubuntu 11.04 x64. We can create capped collection with negative size. And we can create such collection multiple times. After that we can't drop it.

import pymongo
 
args = {"name": "tmpcoll2", "options": { "capped": True, "size":-1, "max":6} }
db = pymongo.Connection().testdb
 
db.create_collection(**args)
# Exception has been generated, but collection created successfully.

Also we can reproduce this bug with shell

MongoDB shell version: 2.0.2
connecting to: test
> use testdb
switched to db testdb
> db.dropDatabase()
{ "dropped" : "testdb", "ok" : 1 }
> db.getCollectionNames()
[ ]
> db.createCollection("tmp1", {capped: true, size: -1})
{
	"errmsg" : "exception: create collection invalid size spec",
	"code" : 10083,
	"ok" : 0
}
> db.getCollectionNames()
[ "tmp1" ]
> db.createCollection("tmp1", {capped: true, size: -1})
{
	"errmsg" : "exception: create collection invalid size spec",
	"code" : 10083,
	"ok" : 0
}
> db.getCollectionNames()
[ "tmp1", "tmp1" ]
> db.createCollection("tmp1", {capped: true, size: -1})
{
	"errmsg" : "exception: create collection invalid size spec",
	"code" : 10083,
	"ok" : 0
}
> db.getCollectionNames()
[ "tmp1", "tmp1", "tmp1" ]
> db.tmp1.drop()
false
> db.getCollectionNames()
[ "tmp1", "tmp1", "tmp1" ]

Comment by Eliot Horowitz [ 23/Dec/11 ]

The minimum size for a capped collection is 4k.
Anything smaller than that will get rounded up.

For the -1 issue - please open a new ticket.

Generated at Sun Sep 24 05:10:32 UTC 2017 using JIRA 7.2.10#72012-sha1:2651463a07e52d81c0fcf01da710ca333fcb42bc.