Learn R Programming

rmongodb (version 1.8.0)

mongo.command: Issue a command to a database on MongoDB server

Description

Issue a command to a MongoDB server and return the response from the server.

Usage

mongo.command(mongo, db, command)

Arguments

mongo
(mongo) A mongo connection object.
db
(string) The name of the database upon which to perform the command.
command
(mongo.bson) An object describing the command.

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

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

Value

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

Details

This function supports any of the MongoDB database commands by allowing you to specify the command object completely yourself.

See http://www.mongodb.org/display/DOCS/List+of+Database+Commands.

See Also

mongo.get.err, mongo.simple.command, mongo.rename, mongo.count, mongo.drop.database, mongo.drop, mongo, mongo.bson.

Examples

Run this code
mongo <- mongo.create()
if (mongo.is.connected(mongo)) {

    # alternate method of renaming a collection
    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "renameCollection", "test.people")
    mongo.bson.buffer.append(buf, "to", "test.humans")
    command <- mongo.bson.from.buffer(buf)
    mongo.command(mongo, "admin", command)

    # use list notation to rename the collection back
    mongo.command(mongo, "admin",
        list(renameCollection="test.humans", to="test.people"))

    # Alternate method of counting people
    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "count", "people")
    mongo.bson.buffer.append(buf, "query", mongo.bson.empty())
    command <- mongo.bson.from.buffer(buf)
    result = mongo.command(mongo, "test", command)
    if (!is.null(result)) {
        iter = mongo.bson.find(result, "n")
        print(mongo.bson.iterator.value(iter))
    }

}

Run the code above in your browser using DataLab