1

I have a mysql table that has postcode and co-ordinates that I would like to import to Mongo. After exporting to csv and importing to MongoDB it looks like this

> db.postcode.findOne()
{
        "_id" : ObjectId("5596d56365f8d76adbae63ed"),
        "postcode" : "AB101XG",
        "latitude" : 57.14416516,
        "longitude" : -2.114847768
}

However, I would like to have the co-ordinates in a array format (assuming it is the format required for geo queries) as below

> db.postcode.findOne()
{
        "_id" : ObjectId("5596d56365f8d76adbae63ed"),
        "postcode" : "AB101XG",
        "loc" : [57.14416516, -2.114847768]
}

Being newbie to Mongo, I am not sure how to achieve it.

1
  • 1
    Can't you post process it? Commented Jul 3, 2015 at 18:49

1 Answer 1

3

Looks like post processing was the only way to go.

> db.postcode.find().forEach( function(r) { 
    r.loc = [r.latitude, r.longitude]; 
    db.postcode.save(r); 
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Just to clarify, db.postcode.save(r) does an update of the document as desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.