Search code examples
mongodbmongoimport

How to import specific csv values as array in MongoDB?


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.


Solution

  • 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); 
      });