ObjectID's in javascript

2,246 views
Skip to first unread message

rpcesar

unread,
Oct 11, 2011, 12:36:58 PM10/11/11
to mongodb-user
I am building an application that often times needs to be cross
referenced, and visualized in many grids (slickgrid) and views, in a
variety of ways. I have facilitated this thus far using a custom built
javascript mongo bridge, which works with a unified data controller.
The system can query specific collections and fields by passing it a
json filter on the client-side. The only real problem I have run into
is handling BSON Id's. for instance, one query we have looks like
this:

{"MyFriends.$id" : ObjectId("4e782c3bcf7a990f2854627d"}

The problem is, this needs to be generated on the javascript (browser)
side, for which ObjectId is unavailable. Passing it as a straight
string also does not work. The query DOES work if we generate /
convert the string to a MongoId server side. However, I am running
into issues attempting this client side. I have considered doing some
sort of server side filter on the terms, but my team is looking or an
easier solution that can be implemented very quickly. Any help would
be appreciated.

Sam Millman

unread,
Oct 11, 2011, 12:52:34 PM10/11/11
to mongod...@googlegroups.com
Maybe this could help: https://github.com/justaprogrammer/ObjectId.js

How exactly are you querying Mongo? You talk about a JS Mongo bridge but does this back onto a server-side language or is it using something like node.js? I would most likely write a abstraction layer to automatically convert "_id" back and forth from/to ObjectId/MongoId. That would be, in my view, the long term solution.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Scott Hernandez

unread,
Oct 11, 2011, 12:52:58 PM10/11/11
to mongod...@googlegroups.com
You can try to use the extended json format: { "$oid" : "<id>" } from
here: http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON

Reply all
Reply to author
Forward
0 new messages