Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying RMongo with ObjectId

Tags:

r

rmongo

Is there a way to query in RMongo with an ObjectId?

Something like:

results <- dbGetQuery(mongo, "users", "{'_id': 'ObjectId('5158ce108b481836aee879f8')'}")

Perhaps by importing a bson library?

like image 473
Dan Blum Avatar asked Oct 20 '22 19:10

Dan Blum


2 Answers

RMongo's dbGetQuery() function is expecting MongoDB Extended JSON syntax for the provided query string.

The MongoDB Extended JSON equivalent of ObjectId("<id>") is { "$oid": "<id>" }:

 results <- dbGetQuery(mongo, "users", "{'_id': { '$oid': '5158ce108b481836aee879f8' }}")
like image 69
Stennie Avatar answered Nov 03 '22 07:11

Stennie


Try the new mongolite package:

library(mongolite)
m <- mongo("users")
m$find('{"_id":{"$oid":"5158ce108b481836aee879f8"}}')
like image 41
Jeroen Ooms Avatar answered Nov 03 '22 08:11

Jeroen Ooms