parsing BSON into JS object

1,146 views
Skip to first unread message

CheekySp

unread,
Nov 6, 2010, 1:44:58 PM11/6/10
to mongodb-user
possibly newbie question..
Is it right that I can't easily create a Javascript object out of
MongoDB query output?
when I looked at MongoDB for some reason I thought it uses JSON for
network transfers.

Now I found out that it uses BSON which is not exactly the same,
and thus my Javascript client requesting data receives BSON string,
which seemingly cannot be parsed to construct a JS object.

Any hints, anyone?

Eliot Horowitz

unread,
Nov 6, 2010, 1:53:04 PM11/6/10
to mongod...@googlegroups.com
What javascript environment are you using?
If its in a browser, you should use one of the REST layers, like
https://github.com/kchodorow/sleepy.mongoose

> --
> 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.
>
>

CheekySp

unread,
Nov 6, 2010, 7:15:24 PM11/6/10
to mongodb-user
Yes, that's JS in browser (actually a browser extension).
But isn't REST means that queries may be freely defined at clientside?
I obviously wouldn't want client users to have unlimited lookup/insert
functionalities in regards to the DB.

I just want the extension to fetch documents matching some criteria
from the server, keeping the structure, but in the form of the JS
object (so that I can manipulate it using standard JS methods in the
client).

Is it possible?
pardon me if I misunderstood the whole REST thing :)


On 6 ноя, 22:53, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> What javascript environment are you using?
> If its in a browser, you should use one of the REST layers, likehttps://github.com/kchodorow/sleepy.mongoose

Eliot Horowitz

unread,
Nov 7, 2010, 8:50:09 PM11/7/10
to mongod...@googlegroups.com
I think you need to explain what your'e trying to do a bit more.
For example, how are you querying the db?

CheekySp

unread,
Nov 9, 2010, 10:28:50 AM11/9/10
to mongodb-user
Hi Elliot,
thanks for replying.
I run MongoDB on NodeJS/Express with Mongoose.

I query it using client-side Javascript using XMLHttpRequest call.


server-side code:
"Vendor.find().all(function(results){
results=JSON.stringify(results);
res.send(results);
"

all Vendor documents are found and returned to client-side
XMLHttpRequest, however, it looke like they are in BSON format -
even though I can do JSON.parse on returned data, the field names are
not registered as properties of JS object.

I feel like I've been neglecting my homework ;)
Am I doing something wrong?

Perhaps to get proper JSON I should use Map/Reduce for such
operations?



On 8 ноя, 06:50, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> I think you need to explain what your'e trying to do a bit more.
> For example, how are you querying the db?
>

Eliot Horowitz

unread,
Nov 9, 2010, 11:01:31 AM11/9/10
to mongod...@googlegroups.com
This is something that mongoose should be doing for you.
You probably need to follow up with them.

CheekySp

unread,
Dec 3, 2010, 8:37:05 AM12/3/10
to mongodb-user
Since I've been receiving emails from people with the same issue I
thought I'd bump this thread and post a solution:
this wasn't a BSON/JSON issue, but rather, as Elliot suggested, a bug
in Moongoose.
Those affected please check
https://github.com/LearnBoost/mongoose/issues/closed#issue/61

Reply all
Reply to author
Forward
0 new messages