1

I have data from mongodb like this from one collection.

/* 1 */
{
    "_id" : ObjectId("5be94355f220b62c7449dc0f"),
    "districts" : [ 
        {
            "name" : "NORTH AND MIDDLE",
            "code" : 632.0
        }, 
        {
            "name" : "EAST",
            "code" : 603.0
        }, 
        {
            "name" : "SOUTH",
            "code" : 602.0
        }
    ],
    "state" : "ISLANDS"
}

/* 2 */
{
    "_id" : ObjectId("5be94355f220b62c7441dc04"),
    "districts" : [ 
        {
            "name" : "Apple",
            "code" : 512.0
        }, 
        {
            "name" : "Ball",
            "code" : 522.0
        }
    ],
    "state" : "GOLD"
}

/* 3 */
{
    "_id" : ObjectId("5eee07816a011d391a45178"),
    "districts" : [ 
        {
            "name" : "DAM",
            "code" : 478.0
        }, 
        {
            "name" : "DEN",
            "code" : 481.0
        }, 
        {
            "name" : "DOG AND CAT",
            "code" : 461.0
        }
    ],
    "state" : "THE NAGAR AND HAVELI"
}

I was given an excel sheet like below as shown with no other information only 2 columns

enter image description here

My work is to add "Short Name of District" for all districts.

I tried below method

var tc = [
    "NORTH AND MIDDLE",
    "EAST",
    "SOUTH",
    "Apple",
    "Ball ",
    "DAM ",
    "DEN ",
    "DOG AND CAT"
]
 

db.dummy.find({"districts.name":{$in:tc}}).forEach(x => {
    x["districts"].forEach( y => {
        if (   
        y.name == "NORTH AND MIDDLE" ){
            y.short_name  = "NAM"
        }
        if (   
            y.name == "EAST" ){
                y.short_name  = "ET"
            }

        if (   
            y.name == "SOUTH" ){
                y.short_name  = "ST"
            }
    })
})

I got the result

/* 1 */
{
    "_id" : ObjectId("5be94355f220b62c7449dc0f"),
    "districts" : [ 
        {
            "name" : "NORTH AND MIDDLE",
            "code" : 632.0,
            "short_name" : "NAM"
        }, 
        {
            "name" : "EAST",
            "code" : 603.0,
        "short_name" : "ET"
        }, 
        {
            "name" : "SOUTH",
            "code" : 602.0,
        "short_name" : "ST"
        }
    ],
    "state" : "ISLANDS"
}

/* 2 */
{
    "_id" : ObjectId("5be94355f220b62c7441dc04"),
    "districts" : [ 
        {
            "name" : "Apple",
            "code" : 512.0,
        "short_name" : "Al"
        }, 
        {
            "name" : "Ball",
            "code" : 522.0
        "short_name" : "BA"
        }
    ],
    "state" : "GOLD"
}

/* 3 */
{
    "_id" : ObjectId("5eee07816a011d391a45178"),
    "districts" : [ 
        {
            "name" : "DAM",
            "code" : 478.0,
        "short_name" : "DA"
        }, 
        {
            "name" : "DEN",
            "code" : 481.0,
        "short_name" : "DN"
        }, 
        {
            "name" : "DOG AND CAT",
            "code" : 461.0
        "short_name" : "DAC"
        }
    ],
    "state" : "THE NAGAR AND HAVELI"
}

Is this is the only method ?? like using if loop for all districts or any other methods are there like using mongodb aggregate or any other javascript methods. It will be helpful if other methods are there as it will be problem to use if loop when there is 730 districts are there. I dont have experience in working with aggregate frameworks so i thought anyone might know other method.

1
  • Aggregation framework is used to process and return computed results from a collection. I think you're looking for bulkwrite method in mongodb apis. docs.mongodb.com/manual/reference/method/… . Also instead of mapping long names to short names using if, you can try to convert csv excel file to a json object. Commented Jun 21, 2020 at 5:23

1 Answer 1

1

You may write a mapping:

const districtNameToShort = {
  'NORTH AND MIDDLE': 'NAM',
  'EAST': 'ET',
  ...
}

Then in your forEach

const districtNameToShort = {
  'NORTH AND MIDDLE': 'NAM',
  'EAST': 'ET'
}
db.dummy.find().forEach(x => {
  db.dummy.update(
    {_id : x._id},
    {$set: {
      districts: x.districts.map(district => {
        district.short_name = districtNameToShort[district.name] || district.name
        return district
      })
    }}
  )
})

Sign up to request clarification or add additional context in comments.

5 Comments

Failed to execute script. Error: TypeError: redeclaration of const districtNameToShort is coming
@AbhishekKV It simply means you copy pasted twice districtName... inside the shell. More importantly what needs to be understood is the usage of mapping to avoid hardcoding the if's. Also: I may if you ask review the update (since I mapped from x.name instead of x.districts.name) but I don't think it is necessary if you understood the idea ?
your script will create new key called short_name below state key i.e. outside districts array but i want inside districts array below each district
@AbhishekKV no much change, I edited to replace at district level instead
Thank you very much now i got to know how mapping works and why error comes in mapping time :)

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.