[SERVER-22983] How to optimize this `for` loop update? Created: 07/Mar/16  Updated: 07/Mar/16  Resolved: 07/Mar/16

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Feng Yu Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

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?



 Comments   
Comment by Stennie Steneker (Inactive) [ 07/Mar/16 ]

Hi,

The SERVER project is for reporting bugs or feature suggestions for the MongoDB server.

For MongoDB-related community support discussion please post on the mongodb-user group (https://groups.google.com/group/mongodb-user) or Stack Overflow. See our Technical Support page for additional support resources.

Thanks,
Stephen

Generated at Thu Feb 08 04:02:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.