Learn R Programming

rmongodb (version 1.8.0)

mongo.bson.buffer.append.long: Append a long valued field onto a mongo.bson.buffer

Description

Append a long value or vector of longs onto a mongo.bson.buffer.

Usage

mongo.bson.buffer.append.long(buf, name, value)

Arguments

buf
(mongo.bson.buffer) The buffer object to which to append.
name
(string) The name (key) of the field appended to the buffer.
value
(double vector) The values(s) to append to the buffer.

If value has a dims attribute of length > 1, any names or dimnames attribute is ignored and a nested array is appended. (Use mongo.bson.buffer.append.object() if you want to preserve dimnames; however, this can't append value as longs).

If value has a names attribute, a subobject is appended and the subfields are given the indicated names.

Otherwise, if more than one element is present in value, the values are appended as a subarray.

In the last case, a single long is appended as the value of the field.

Value

TRUE if successful; otherwise, FALSE if an error occured appending the data.

Details

Note that since R has no long (64-bit integer) type, doubles are used in R, but are converted to 64-bit values when stored in the buffer; some loss of precision may occur.

This is the only case in which mongo.bson.buffer.append() cannot make the proper guess about what type to encode into the buffer. You must call mongo.bson.buffer.append.long() explicitly; otherwise, doubles are appended.

See Also

mongo.bson, mongo.bson.buffer, mongo.bson.buffer.append.

Examples

Run this code
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append.long(buf, "YearSeconds",
    365.24219 * 24 * 60 * 60)
b <- mongo.bson.from.buffer(buf)

# The above produces a BSON object of the form:
# { "YearSeconds" : 31556925 }

buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append.long(buf, "longs",
    c(1, 9087654321, 1234567809))
b <- mongo.bson.from.buffer(buf)

# The above produces a BSON object of the form:
# { "longs" : [1, 9087654321, 1234567809] }

buf <- mongo.bson.buffer.create()
distances <- c(473, 133871000, 188178313)
names(distances) <- c("Sol", "Proxima Centari", "Bernard's Star")
mongo.bson.buffer.append.long(buf, "Stars", distances)
b <- mongo.bson.from.buffer(buf)

# The above produces a BSON object of the form:
# { "Stars" : { "Sol" : 474,
#               "Proxima Centari" : 133871000,
#               "Bernard's Star"  : 188178313 } }

Run the code above in your browser using DataLab