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

Description Usage Arguments Details Value See Also Examples

View source: R/mongo_index.R

Description

Add an index to a collection.

Usage

1

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.

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().

options

(integer vector) Optional flags governing the operation:

  • mongo.index.unique

  • mongo.index.drop.dups

  • mongo.index.background

  • mongo.index.sparse

Details

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

Value

NULL if successful; otherwise, a mongo.bson object describing the error.
mongo.get.server.err() or mongo.get.server.err.string() may alternately be called in this case instead of examining the returned object.

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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))
}

jonkatz2/rmongodb documentation built on May 19, 2019, 7:30 p.m.