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

Description Usage Arguments Details Value See Also Examples

View source: R/mongo.R

Description

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

Usage

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

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.

Value

NULL if the command failed. mongo.get.err() may be MONGO_COMMAND_FAILED.

(mongo.bson) The server's response if successful.

See Also

mongo.get.err,
mongo.simple.command,
mongo.rename,
mongo.count,
mongo.drop.database,
mongo.drop,
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
25
26
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))
    }

}

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