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