Database Reference
In-Depth Information
You need to be very careful when using the dropdups option because it will result in documents being
deleted from your collection.
Warning
You should be extremely aware of the data in your collection before using this option; otherwise, you might get
unexpected (not to mention unwanted) behavior. It is an extremely good idea to run a group query against a collection
for which you intend to make a key unique; this will enable you to determine the number of documents that would be
regarded as duplicates before you execute this option.
Creating Sparse Indexes with {sparse:true}
Sometimes it can be worthwhile to create an index of only documents that contain an entry for a given field. For
example, let's say you want to index emails, and you know that not all emails will have a CC or a BCC field. If you
create an index on CC or BCC, then all documents would be added with a “null” value, unless you specify a sparse
index. This can be a space-saving mechanism as you only index on valid documents rather than all documents. Of
course, this has an impact on any queries that are run and use the sparse index; as there are may be documents that
are not evaluated in the query.
TTL Indexes
In computing terms, TTL (Time To Live) is a way of giving a particular piece of data or a request a lifespan by
specifying a point at which it becomes invalid. This can be useful for data stored within your MongoDB instance,
too, as often it is nice to have old data automatically deleted. In order to create a TTL index you must add the
expireAfterSeconds flag and a seconds value to a single (noncompound) index. This will indicate that any document
that has the indexed field greater than the given TTL value will be deleted when the TTL deletion task next executes.
As the deletion task is run once every 60 seconds, there can be some delay before your old documents are removed.
the field being indexed must be a BSOn date type; otherwise, it will not be evaluated to be deleted. a BSOn
date will appear as ISODate when queried from the shell as in the next example.
Warning
Suppose for example that we want to automatically delete anything from the blog's comments collection, which
has a created timestamp over a certain age. Take this example document from the comments collection:
>db.comments.find();
{
"_id" : ObjectId("519859b32fee8059a95eeace"),
"author" : "david",
"body" : "foo",
"ts" : ISODate("2013-05-19T04:48:51.540Z"),
"tags" : [ ]
}
Let's say we want to have any comment older than 28 days deleted. We work out that 28 days is 2,419,200 seconds
long. Then we create the index as follows:
>db.comments.ensureIndex({ts:1},{ expireAfterSeconds: 2419200})
 
 
Search WWH ::




Custom Search