mongo.bson.to.Robject(b)
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:
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:
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.
mongo.bson.from.list
, mongo.bson.to.list
, mongo.bson.
b <- mongo.bson.from.JSON('{"name":"Fred", "city":"Dayton"}')
l <- mongo.bson.to.Robject(b)
print(l)
Run the code above in your browser using DataLab