rmongodb (version 1.8.0)

mongo.index.create: Add an index to a collection

Description

Add an index to a collection.

Usage

mongo.index.create(mongo, ns, key, options = 0L)

Arguments

mongo
(mongo) A mongo connection object.
ns
(string) The namespace of the collection to which to add an index.
key
An object enumerating the fields in order which are to participate in the index. This object may be a vector of strings listing the key fields or a mongo.bson object containing the key fields in the desired order.

A

options
(integer vector) Optional flags governing the operation:

Value

Details

See http://www.mongodb.org/display/DOCS/Indexes.

See Also

mongo.find, mongo.find.one, mongo.insert, mongo.update, mongo.remove, mongo, mongo.bson.

Examples

Run this code
mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
    # Add a city index to collection people in database test
    b <- mongo.index.create(mongo, "test.people", '{"city":1}')
    if (!is.null(b)) {
        print(b)
        stop("Server error")
    }

    # Add an index to collection people in database test
    # which will speed up queries of age followed by name
    b <- mongo.index.create(mongo, "test.people", c("age", "name"))

    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "age", 1L)
    mongo.bson.buffer.append(buf, "name", 1L)
    key <- mongo.bson.from.buffer(buf)

    # add an index using an alternate method of specifying the key fields
    b <- mongo.index.create(mongo, "test.people", key)

    # create an index using list of that enumerates the key fields
    b <- mongo.index.create(mongo, "test.cars", list(make=1L, model=1L))
}

Run the code above in your browser using DataLab