mongo.bson.to.Robject: Convert a mongo.bson object to an R object.

Description Usage Arguments Details Value See Also Examples

View source: R/bson_convert.R

Description

Convert a mongo.bson object to an R object.

Usage

1

Arguments

b

(mongo.bson) The mongo.bson object to convert.

Details

Note that this function and mongo.bson.from.list() do not always perform inverse conversions since mongo.bson.to.Robject() will convert objects and subobjects to atomic vectors if possible.

This function is somewhat schizophrenic depending on the types of the fields in the mongo.bson object. If all fields in an object (or subobject/array) can be converted to the same atomic R type (for example they are all strings or all integer, you'll actually get out a vector of the atomic type with the names attribute set.

For example, if you construct a mongo.bson object like such:

1
2
b <- mongo.bson.from.JSON('{"First":"Joe", "Last":"Smith"}')
l <- mongo.bson.to.Robject(b)

You'll get a vector of strings out of it which may be indexed by number, like so:

print(l[1]) # display "Joe"

or by name, like so:

print(l[["Last"]]) # display "Smith"

If, however, the mongo.bson object is made up of disparate types like such:

1
2
b <- mongo.bson.from.JSON('{"First":"Joe Smith", "Last":21.5}')
l <- mongo.bson.to.Robject(b)

You'll get a true list (with the names attribute set) which may be indexed by number also:

print(l[1]) # display "Joe Smith"

or by name, in the same fashion as above, like so

print(l[["Name"]]) # display "Joe Smith"

but also with the $ operator, like so:

print(l$age) # display 21.5

Note that mongo.bson.to.Robject() operates recursively on subobjects and arrays and you'll get lists whose members are lists or vectors themselves. See mongo.bson.value() for more information on the conversion of component types.

This function also detects the special wrapper as output by mongo.bson.buffer.append.object() and will return an appropriately attributed object.

Perhaps the best way to see what you are going to get for your particular application is to test it.

Value

Best guess at an appropriate R object representing the mongo.bson object.

See Also

mongo.bson.from.list,
mongo.bson.to.list,
mongo.bson.

Examples

1
2
3
4
b <- mongo.bson.from.JSON('{"name":"Fred", "city":"Dayton"}')

l <- mongo.bson.to.Robject(b)
print(l)

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