Tuesday 16 May 2017

Mongoose data migration / old data in mongoDB to new data

data in production is something like this.

{
    "_id" : ObjectId("590c5f33e56b332e50d4ed8b"),
    "cdrname" : "location-test-CDR",
    "app_id" : "12746703360070445",
    "updated_at" : ISODate("2017-05-05T16:47:07.529+05:30"),
    "created_at" : ISODate("2017-05-05T16:47:07.529+05:30"),
    "offer" : [
        [
            {
                "phonenumber" : "8887713618",
                "alias" : "Location-test-Offer-1",
                "offername" : "Location-test-Offer-1",
                "_id" : "590c5ea8e56b332e50d4ed88"
            },
            {
                "phonenumber" : "8888856112",
                "alias" : "Location-test-offer-2",
                "offername" : "Location-test-offer-2",
                "_id" : "590c5edfe56b332e50d4ed89"
            }
        ]
    ],
    "verifyLocation" : [
        true
    ],
    "percent" : [ ],
    "askZipCode" : [ ],
    "message" : [
        "01 - Ok Jaanu (128 Kbps) - DownloadMing.SE.mp3"
    ],
    "mtype" : [
        "voice"
    ],
    "checked" : [
        true
    ],
    "trafficdistby" : [
        "Offer"
    ],
    "__v" : NumberInt("1")
}

but due to my new schema change i want something like this.

{
    "_id" : ObjectId("590c5f33e56b332e50d4ed8b"),
    "cdrname" : "location-test-CDR",
    "app_id" : "12746703360070445",
    "updated_at" : ISODate("2017-05-05T16:47:07.529+05:30"),
    "created_at" : ISODate("2017-05-05T16:47:07.529+05:30"),
    "offer" : [
        {
            "0" : {
                "phonenumber" : "8887713618",
                "alias" : "Location-test-Offer-1",
                "offername" : "Location-test-Offer-1",
                "_id" : "590c5ea8e56b332e50d4ed88"
            },
            "1" : {
                "phonenumber" : "8888856112",
                "alias" : "Location-test-offer-2",
                "offername" : "Location-test-offer-2",
                "_id" : "590c5edfe56b332e50d4ed89"
            },
            "defaultLocationOffer" : {
                "phonenumber" : "8886666494",
                "alias" : "location-test-Offer-default",
                "offername" : "location-test-Offer-default",
                "_id" : "590c5f12e56b332e50d4ed8a"
            }
        },
        {
            "0" : {
                "phonenumber" : "8887713618",
                "alias" : "Location-test-Offer-1",
                "offername" : "Location-test-Offer-1",
                "_id" : "590c5ea8e56b332e50d4ed88"
            },
            "1" : {
                "phonenumber" : "8888856112",
                "alias" : "Location-test-offer-2",
                "offername" : "Location-test-offer-2",
                "_id" : "590c5edfe56b332e50d4ed89"
            },
            "defaultLocationOffer" : {
                "phonenumber" : "8886666494",
                "alias" : "location-test-Offer-default",
                "offername" : "location-test-Offer-default",
                "_id" : "590c5f12e56b332e50d4ed8a"
            }
        }
    ],
    "verifyLocation" : [
        true
    ],
    "percent" : [ ],
    "askZipCode" : [ ],
    "message" : [
        "01 - Ok Jaanu (128 Kbps) - DownloadMing.SE.mp3"
    ],
    "mtype" : [
        "voice"
    ],
    "checked" : [
        true
    ],
    "trafficdistby" : [
        "Offer"
    ],
    "__v" : NumberInt("1")
}

Any idea on how to migrate old schema to new.I am using mongoose as ORM with HapiJS

i tried migrate-mongoose (https://github.com/balmasi/migrate-mongoose) could not figure it out.



via Nesar

No comments:

Post a Comment