6

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?

Dan Blum
  • 61
  • 3

3 Answers3

3

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' }}")
Stennie
  • 63,885
  • 14
  • 149
  • 175
  • Thanks a lot, I had spent hours figuring this out. Is there any documentation where I can look up when stuck with issues like these? – Kumar Deepak Sep 14 '15 at 07:11
  • There is some documentation included as part of the RMongo package (eg: https://cran.r-project.org/web/packages/RMongo/RMongo.pdf), but it's a bit thin on details & examples. In general RMongo relies on standard MongoDB operators & Extended JSON syntax as described in the MongoDB manual (http://docs.mongodb.org). If you can't find an answer, I'd suggest asking a new question on StackOverflow (or perhaps add a bounty to an existing question that hasn't received enough attention). – Stennie Sep 14 '15 at 07:15
1

Try the new mongolite package:

library(mongolite)
m <- mongo("users")
m$find('{"_id":{"$oid":"5158ce108b481836aee879f8"}}')
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
0

mongo.oid.from.string {rmongodb}

Create a mongo.oid object ftom a string Package: rmongodb Version: 1.5.3 Description Create from a 24-character hex string a mongo.oid object representing a MongoDB Object ID.

Usage mongo.oid.from.string(hexstr) Arguments hexstr (string) 24 hex characters representing the OID. Note that although an error is thrown if the length is not 24, no error is thrown if the characters are not hex digits; you'll get zero bits for the invalid digits.

Details See http://www.mongodb.org/display/DOCS/Object+IDs

Values

mgcdanny
  • 1,082
  • 1
  • 13
  • 20