In R with mongolite, how to return _id and extract timestamp from _id

69 Views Asked by At

We have the following R code which fetches an entire collection from our mongodb cluster

mongo_uri <- "mongodb+srv://username:[email protected]/dbname?retryWrites=true&w=majority"
con <- mongolite::mongo('users', db = 'dbname', url = mongo_uri)
users <- con$find('{}')

In the following code, the _id is not returned. However, we want the _id returned, and then we want to use the _id along with the ObjectId.getTimestamp function to extract the date that the document was added to the collection.

Is this possible to do in R with mongolite?

1

There are 1 best solutions below

0
Canovice On

Solved this one on my own.

users <- con$find('{}', fields = '{}')
oid_to_timestamp(users$`_id`[1])