tc / RMongo

R client to interface with MongoDB
102 stars 34 forks source link

How can we specify a query where the value is an ObjectId #35

Closed krdeepakio closed 9 years ago

krdeepakio commented 9 years ago

Works fine dbGetQueryForKeys(mongo, "tables", "{}", "{'source':1}", 0, 1)

Empty result if I specify a valid query with value of type ObjectId. Both of these give empty result. dbGetQueryForKeys(mongo, "tables", "{'source':'6dc63c46310a3d297c4cc6b4'}", "{'source':1}", 0, 1) dbGetQueryForKeys(mongo, "tables", "{'source':6dc63c46310a3d297c4cc6b4}", "{'source':1}", 0, 1)

'source':ObjectId('...') gives syntax error.

stennie commented 9 years ago

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>" }, so your query should be:

dbGetQueryForKeys(mongo, "tables",

   # JSON query
   "{'source': { '$oid': '6dc63c46310a3d297c4cc6b4' }}",

   # Projection document (fields to return)
   "{'source':1}",

   # (optional) skip, limit
   0, 1
)