Database Reference
In-Depth Information
If you're using a standard collection, then you'll need to be sure to remove old queue
entries. It's possible to remove them at processing time using findAndModify 's
{remove: true} option. But some applications may want to postpone removal for a
later time, once the processing is complete.
Capped collections may also serve as the basis for a worker queue. Without the
default index on _id , a capped collection has potentially faster insert speed, but the
difference will be negligible for most applications. The other potential advantage is
automatic deletion. But this feature is a double-edged sword: you'll have to make sure
that the collection is large enough to prevent unprocessed entries from aging out.
Thus if you do use a capped collection, make it extra large. The ideal collection size
will depend on your queue write throughput and the average payload size.
Once you've decided on the size of capped collection to use, the schema, index,
and findAndModify will be identical to those of the standard collection just described.
B.1.6
Dynamic attributes
MongoDB's document data model is useful for representing entities whose attributes
vary. Products are the canonical example of this, and you saw some ways of modeling
these attributes earlier in the topic. One viable way to model these attributes is to
scope them to a sub-document. In a single products collection, you can then store dis-
parate product types. You might store a set of headphones
{ _id: ObjectId("4d669c225d3a52568ce07646")
sku: "ebd-123"
name: "Hi-Fi Earbuds",
type: "Headphone",
attrs: { color: "silver",
freq_low: 20,
freq_hi: 22000,
weight: 0.5
}
}
and an SSD drive:
{ _id: ObjectId("4d669c225d3a52568ce07646")
sku: "ssd-456"
name: "Mini SSD Drive",
type: "Hard Drive",
attrs: { interface: "SATA",
capacity: 1.2 * 1024 * 1024 * 1024,
rotation: 7200,
form_factor: 2.5
}
}
If you need to frequently query on these attributes, you can create sparse indexes for
them. For example, you can optimize for range queries in headphone frequency
response:
Search WWH ::




Custom Search