jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

Cannot insert data frame including geospatial object #122

Closed stPhena closed 6 years ago

stPhena commented 6 years ago

I have a dataset of approximately 18 million rows that I am trying to insert into a MongoDB collection. The important part of the dataset looks like this:

# A tibble: 1,000 x 6
      id    lon   lat depth date                coords        
   <int>  <dbl> <dbl> <dbl> <dttm>              <list>        
 1   656 - 25.5  17.9  35.0 1969-11-17 11:00:00 <S3: geopoint>
 2   840  145   -13.3  NA   1982-07-02 10:00:00 <S3: geopoint>
 3   848 - 70.7  40.6  66.0 1980-04-07 03:34:48 <S3: geopoint>

I have created the coords column with geojson::point(), with the intention of having MongoDB natively treat it as a geospatial object. Seen from the other side, it looks like this:

Observations: 1,000
Variables: 6
$ id              <int> 656, 840, 848, 927, 1012, 1023, 1037, 1041, 1044, 1050, 1051, 1054, 1057,...
$ lon             <dbl> -25.47, 145.45, -70.72, -57.97, -75.77, -67.10, 93.00, -69.30, -58.62, -7...
$ lat             <dbl> 17.8742, -13.3000, 40.6300, 46.7100, 34.4200, 43.8700, 10.0000, 42.1500, ...
$ depth           <dbl> 35, NA, 66, 453, 344, 85, NA, 193, 457, 36, 237, 191, 86, 199, 73, 165, 1...
$ date            <dttm> 1969-11-17 11:00:00, 1982-07-02 10:00:00, 1980-04-07 03:34:48, 1988-02-0...
$ coords          <list> [{"type":"Point","coordinates":[-25.47, 17.8742]}, {"type":"Point","coor...

When I try to send data to the Mongo collection, I get this error:

> myMongo$insert(upload)
Error: No method asJSON S3 class: geojson

Among the toJSON() parameters I could not find anything to manage these objects. Is mongolite able to insert data frames with a geospatial feature? Edit: In other words, can we specify two columns to be treated as (lon, lat) and thus be parsed as such into the JSON? Thanks in advance.

jeroen commented 6 years ago

Thanks. Could you maybe provide a smaller example to reproduce this issue?

stPhena commented 6 years ago

Here are 10 data points, with their longitude and latitude fields (format is CSV): EXAMPLE

jeroen commented 6 years ago

Can you include the code please to reproduce the problem?

jeroen commented 6 years ago

This works fine for me:

mydata <- readr::read_csv('https://github.com/jeroen/mongolite/files/2283061/extract.txt')
m <- mongolite::mongo()
m$insert(mydata)
stPhena commented 6 years ago

It does, but MongoDB does not recognize the uploaded data as coordinates. Nevermind, I have since abandoned MongoDB and I have transitioned to other DB technologies. It is now difficult for me to reproduce the code that I had used to create the S3 geopoint class.