Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-22983

How to optimize this `for` loop update?

    • Type: Icon: Question Question
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: JavaScript
    • None

      I want to replace the collection field based on a map table, like

      old_src new_src
      1 448
      5 449
      ... ...

      So I write a very simple JavaScript.

      db_name = 'mydb'
      collection_name = 'mycoll'
      
      src_map = [
          [1,448],
          [5,449],
          [6,450],
          [8,451],
          [9,452],
          [10,453],
          [11,454],
          [12,455],
          [14,456],
          [15,457],
          [16,458],
          [17,459],
          [18,460],
          [20,461],
          [22,462],
          [23,463],
          [24,464],
          [25,465],
          [26,466],
          [27,467],
          [29,468]
      ]
      
      db = db.getSiblingDB(db_name)
      for (x in src_map) {
          old2new_map = src_map[x];
          print("Changing src: " + String(old2new_map[0]) + " --> " + String(old2new_map[1]) + " ... ");
          db[collection_name].update({"src":old2new_map[0]},{$set:{"src":old2new_map[1]}},{multi:true});
          print("Done.")
      }
      

      I use update function within a for loop to do this. But it's so slow. This collection contains more than 1000000 objects, and each update will execute more than 10 mins(even if src field has a index).

      How to optimize this script? Is there any way to update the whole may in a single function? Or use multi thread way to let the update function parallelly?

            Assignee:
            Unassigned Unassigned
            Reporter:
            abcfy2 Feng Yu
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: