jeroen / mongolite

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

How to insert Date #103

Closed sonijem closed 6 years ago

sonijem commented 7 years ago

Hi I'm trying insert date but it is only taking as string.

mongo$update(query = paste0('{"_id": ', c, ' }'), update = paste0('{"$addToSet": {"values": { "date_data": "ISODate("',dat,'")" } } }'))

If i remove quotes from value: "ISODate("',dat,'")", its is giving invalid json object error and with quote it inserts as string.

jeroen commented 6 years ago

A date in mongo json looks like this: { "$date" : "2017-01-01T00:00:00Z" }. You can also use regular R datetime objects. Here is an example:

# Create some dates
df <- data.frame(
  dates = Sys.time() + 1:4
)

# Insert data with dates
m <- mongolite::mongo("test")
m$insert(df)

# Read data back and check dates
out <- m$find()
out$dates

See example from documentation.

sonijem commented 6 years ago

Thanks, Jeroren.... 👍