rmongodb (version 1.8.0)

mongo.insert: Add record to a collection

Description

Add record to a collection.

Usage

mongo.insert(mongo, ns, b)

Arguments

mongo
(mongo) a mongo connection object.
ns
(string) namespace of the collection to which to add the record.
b
(mongo.bson) The record to add.

In addition, b may be a list which will be converted to a mongo.bson object by mongo.bson.from.list().

Value

TRUE if the command was successfully sent to the server; otherwise, FALSE.mongo.get.last.err() may be examined to verify that the insert was successful on the server if necessary.

Details

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

See Also

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

Examples

Run this code
mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
    ns <- "test.people"

    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "name", "Joe")
    mongo.bson.buffer.append(buf, "age", 22L)
    b <- mongo.bson.from.buffer(buf)
    mongo.insert(mongo, ns, b)

    # do the same thing in shorthand:
    mongo.insert(mongo, ns, list(name="Joe", age=22L))
}

Run the code above in your browser using DataLab