Learn R Programming

rmongodb (version 1.8.0)

mongo.index.TTLcreate: Add a time to live (TTL) index to a collection

Description

Add a time to live (TTL) index to a collection

Usage

mongo.index.TTLcreate(mongo, ns, key, expireAfterSeconds, index_name = NULL)

Arguments

mongo
(mongo) A mongo connection object.
ns
(string) The namespace of the collection to add a TTL index to.
key
(mongo.bson) The desired field(s) to use as the basis for expiration time. The field should be of type 'Date'.

Alternately, key may be a list which will be converted to a mongo.bson object by mongo.bson.from.list().

Alternately, key may be a valid JSON character string which will be converted to a mongo.bson object by mongo.bson.from.JSON().

expireAfterSeconds
(Numeric or Integer) The time in seconds after which records should be removed.
index_name
(string) The name of the index to be created.

Value

NULL if the command failed. mongo.get.err() may be MONGO_COMMAND_FAILED.(mongo.bson) The server's response if successful.

Details

See http://docs.mongodb.org/manual/tutorial/expire-data.

See Also

mongo.index.create

Examples

Run this code
mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
 for (i in 1:10) mongo.insert(mongo, ns = 'test.testTTL', b = list(a = i,  last_updated = i))
 res_bson <- mongo.index.TTLcreate (mongo, ns = 'test.testTTL', key = list(last_updated = 1),
                                     expireAfterSeconds = 3600, index_name = 'last_updated_1')
 print(res_bson);
 mongo.drop(mongo, ns = 'test.testTTL')
}
mongo.destroy(mongo);

Run the code above in your browser using DataLab