rmongodb (version 1.8.0)

mongo.insert.batch: Add multiple records to a collection

Description

Add multiple records to a collection. This function eliminates some network traffic and server overhead by sending all the records in a single message.

Usage

mongo.insert.batch(mongo, ns, lst)

Arguments

mongo
(mongo) a mongo connection object.
ns
(string) namespace of the collection to which to add the record.
lst
A list of (mongo.bson) records to add.

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, 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", "Dave")
    mongo.bson.buffer.append(buf, "age", 27L)
    x <- mongo.bson.from.buffer(buf)

    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "name", "Fred")
    mongo.bson.buffer.append(buf, "age", 31L)
    y <- mongo.bson.from.buffer(buf)

    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "name", "Silvia")
    mongo.bson.buffer.append(buf, "city", 24L)
    z <- mongo.bson.from.buffer(buf)
    mongo.insert.batch(mongo, ns, list(x, y, z))
}

Run the code above in your browser using DataLab