Best practice to transform JSON with MongoDB

246 views
Skip to first unread message

D Boyd

unread,
Nov 1, 2010, 7:39:46 PM11/1/10
to mongodb-user
Greetings all:
I am pretty new to Mongo and had a question I hope the group can
help with.

I have a number of records in the DB now, that I would like to
transform server side from their
current structure into a GeoJSON compliant structure.
Basically, the records are of the form:

{ id: "id1", att1: "att value", att2: "att 2 value", lat: "34.33",
lon: "22.34" }

I need to transform that into GeoJSON of the form:

{ "type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
[22.34, 34.33]
]
},
"properties": {
"att1": "att value",
"att2": "att 2 value"
}
}

In my searches I found a couple of interesting Javascript functions
(jsonT, jsonPath) at http://goessner.net/articles/JsonPath/

I have successfully stored them inside my mongo instance and was even
able from the console to execute jsonPath on a result set.
However the whole syntax with db.eval is klunky and I have read about
issues with
it blocking the database.

There are several brute force approaches that can be written in
javascript or java to do the work but
before I tackled those I thought I would ask the list if there was
some best practice or more elegant
approach.

Kyle Banker

unread,
Nov 3, 2010, 10:33:47 AM11/3/10
to mongod...@googlegroups.com
I think that the fastest and best approach here would be to query your
collection in batches, transform using Java, and then bulk-insert into
a new collection.

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

Reply all
Reply to author
Forward
0 new messages