Database Reference
In-Depth Information
Documents already added to a capped collection can be updated, but they must not grow in size. the update
will fail if they do. Deleting documents from a capped collection is also not possible; instead, the entire collection must be
dropped and re-created if you want to do this. You will learn more about dropping a collection later in this chapter.
Note
You can also limit the number of items added into a capped collection using the max: parameter when you create
the collection. However, you must take care to ensure that there is enough space in the collection for the number of
items you want to add. If the collection becomes full before the number of items has been reached, the oldest item in
the collection will be removed. The MongoDB shell includes a utility that lets you see the amount of space used by an
existing collection, whether it's capped or uncapped. You invoke this utility using the validate() function. This can
be particularly useful if you want to estimate how large a collection might become.
As stated previously, you can use the max: parameter to cap the number of items that can be inserted into a
collection, as in this example:
> db.createCollection("audit100", { capped:true, size:20480, max: 100})
{ "ok" : 1 }
Next, use the validate() function to check the size of the collection:
> db.audit100.validate()
{
"ns" : "media.audit100",
"result" : "
validate
capped:1 max:100
firstExtent:0:54000 ns:media.audit100
lastExtent:0:54000 ns:media.audit100
# extents:1
datasize?:0 nrecords?:0 lastExtentSize:20736
padding:1
first extent:
loc:0:54000 xnext:null xprev:null
nsdiag:media.audit100
size:20736 firstRecord:null lastRecord:null
capped outOfOrder:0 (OK)
0 objects found, nobj:0
0 bytes data w/headers
0 bytes data wout/headers
deletedList: 1100000000000000000
deleted: n: 2 size: 20560
nIndexes:0
",
"ok" : 1,
"valid" : true,
"lastExtentSize" : 20736
}
The resulting output shows that the table (named audit100 ) is a capped collection with a maximum of 100 items
to be added, and it currently contains zero items.
 
 
Search WWH ::




Custom Search