Search code examples
rmongodbmongolite

Get ObjectID in mongolite R library


I can successfully retrieve data from my mongoDB instance but need to re-use the objectID for a depending query.

The following code seems to get my entire object but NOT the id. What am I missing?

# Perform a query and retrieve data
mongoOBj <- m$find('{"em": "[email protected]"}')

Solution

  • If you look at the documentation you see that the find method takes a field argument, where you specify the fields you want:

    find(query = ’{}’, fields = ’{"_id" : 0}’, sort = ’{}’, skip = 0, limit = 0, handler = NULL, pagesize = NULL)
    

    So in your case it will be something like

    mongoOBj <- m$find(query = '{"em": "[email protected]"}', field = '{"_id": 1}')