Database Reference
In-Depth Information
insertion order.
8
Your previous query returned items from your collection in forward
natural order. To return them in reverse natural order, you must use the
$natural
sort operator:
> db.user.actions.find().sort({"$natural": -1});
In addition to ordering documents naturally and eschewing indexes, capped collec-
tions limit certain
CRUD
operations. For one, you can't delete individual documents
from a capped collection; nor can you perform any update that will increase the size
of a document.
9
S
YSTEM
COLLECTIONS
Part of MongoDB's design lies in its own internal use of collections. Two of these spe-
cial system collections that are always present are
system.namespaces
and
system
.indexes
. You can query the former to see all the namespaces defined for the current
database:
> db.system.namespaces.find();
{ "name" : "garden.products" }
{ "name" : "garden.system.indexes" }
{ "name" : "garden.products.$_id_" }
{ "name" : "garden.user.actions", "options" :
{ "create": "user.actions", "capped": true, "size": 1024 } }
The latter collection,
system.indexes
, stores each index definition for the current
database. To see a list of indexes you've defined for the
garden
database, just query
the collection:
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "garden.products", "key" : { "_id" : 1 } }
system.namespaces
and
system.indexes
are both standard collections, but
MongoDB uses capped collections for replication. Each member of a replica set logs
all its writes to a special capped collection called
oplog.rs
. Secondary nodes then
read from this collection sequentially and apply new operations to themselves. We'll
discuss this systems collection in more detail in chapter 9.
4.3.3
Documents and insertion
We'll round out this chapter with some details on documents and their insertion.
D
OCUMENT
SERIALIZATION
,
TYPES
,
AND
LIMITS
As stated in the previous chapter, all documents must be serialized to
BSON
before
being sent to MongoDB; they're later deserialized from
BSON
by the driver into the
language's native document representation. Most of the drivers provide a simple
interface for serializing to and from
BSON
, and it's useful knowing how this works for
8
The natural order is the order in which documents are stored on disk.
9
Since capped collections were originally designed for logging, there was no need to implement the deletion
or updating of documents, as this would've complicated the code responsible for aging out old documents.
Without these features, capped collections preserve the simplicity and efficiency they were designed for.