gerald-lindsly / rmongodb

R driver for MongoDB
http://cnub.org/rmongodb.ashx
Apache License 2.0
83 stars 44 forks source link

NAs in logical variable appear as true in BSON #12

Closed nbest937 closed 11 years ago

nbest937 commented 11 years ago

Gerald, thanks for the rmongodb package. I think I may have stumbled upon a problem. I hope this is constructive. As far as I can tell, a value of NA in a logical variable that is a member of a list gets translated to to 'true' in the BSON data structure. Here is what I am looking at:

> str(data)
List of 23
 $ service_request_type                                              : chr "Vacant/Abandoned Building"
 $ service_request_number                                            : chr "13-00115287"
 $ date_service_request_was_received                                 : chr "01/30/2013"
 $ location_of_building_on_the_lot_if_garage_change_type_code_to_bgd_: logi NA
 $ is_the_building_dangerous_or_hazardous_                           : logi NA
 $ is_building_open_or_boarded_                                      : logi NA
 $ if_the_building_is_open_where_is_the_entry_point_                 : logi NA
 $ is_the_building_currently_vacant_or_occupied_                     : logi NA
 $ is_the_building_vacant_due_to_fire_                               : logi NA
 $ any_people_using_property_homeless_childen_gangs_                 : logi NA
 $ address_street_number                                             : int 3626
 $ address_street_direction                                          : chr "W"
 $ address_street_name                                               : chr "58TH"
 $ address_street_suffix                                             : chr "ST"
 $ zip_code                                                          : int 60629
 $ x_coordinate                                                      : num 1153148
 $ y_coordinate                                                      : num 1865909
 $ ward                                                              : int 14
 $ police_district                                                   : int 8
 $ community_area                                                    : int 62
 $ latitude                                                          : num 41.8
 $ longitude                                                         : num -87.7
 $ location                                                          : chr "(41.787898459688606, -87.71400987887118)"
> mongo.bson.from.list( data)
        service_request_type : 2         Vacant/Abandoned Building
        service_request_number : 2       13-00115287
        date_service_request_was_received : 2    01/30/2013
        location_of_building_on_the_lot_if_garage_change_type_code_to_bgd_ : 8   true
        is_the_building_dangerous_or_hazardous_ : 8      true
        is_building_open_or_boarded_ : 8         true
        if_the_building_is_open_where_is_the_entry_point_ : 8    true
        is_the_building_currently_vacant_or_occupied_ : 8        true
        is_the_building_vacant_due_to_fire_ : 8          true
        any_people_using_property_homeless_childen_gangs_ : 8    true
        address_street_number : 16       3626
        address_street_direction : 2     W
        address_street_name : 2          58TH
        address_street_suffix : 2        ST
        zip_code : 16    60629
        x_coordinate : 1         1153148.248780
        y_coordinate : 1         1865909.097719
        ward : 16        14
        police_district : 16     8
        community_area : 16      62
        latitude : 1     41.787898
        longitude : 1    -87.714010
        location : 2     (41.787898459688606, -87.71400987887118)
> 

Does anything jump out at you here? The input list was created using an as.list( read.csv( text= )) call. Pretty straightforward I think. Thanks for taking a look. Please let me know if I can help work this out in any way.

Maybe I should try to find a record with non-NA values and see what it does then? Will post follow-up.

nbest937 commented 11 years ago

Perhaps this is a better/simpler illustration, building on examples from your docs:

> buf <- mongo.bson.buffer.create()
> mongo.bson.buffer.append.bool(buf, "bools", c(TRUE, FALSE, FALSE))
> [1] TRUE
> b <- mongo.bson.from.buffer(buf)
> b
        bools : 4
                0 : 8    true
                1 : 8    false
                2 : 8    false

> buf <- mongo.bson.buffer.create()
> mongo.bson.buffer.append.bool(buf, "bools", c(TRUE, FALSE, NA))
[1] TRUE
> b <- mongo.bson.from.buffer(buf)
> b
        bools : 4
                0 : 8    true
                1 : 8    false
                2 : 8    true

Would you call this a bug or do I need to come up with a work-around to get nulls in there?

gerald-lindsly commented 11 years ago

You'll need to come up with a work around. "NA" is an 'R-ism'. There is currently no way to store such a value for a boolean using BSON. As far as rmongodb and MongoDB and BSON go, non-zero is true.