MongoDB - what is the fastest way to update all records in a collection?

前端 未结 5 1618
野性不改
野性不改 2020-12-07 18:43

I have a collection with 9 million records. I am currently using the following script to update the entire collection:

simple_update.js

db.my         


        
5条回答
  •  失恋的感觉
    2020-12-07 19:36

    There are two things that you can do.

    1. Send an update with the 'multi' flag set to true.
    2. Store the function server-side and try using server-side code execution.

    That link also contains the following advice:

    This is a good technique for performing batch administrative work. Run mongo on the server, connecting via the localhost interface. The connection is then very fast and low latency. This is friendlier than db.eval() as db.eval() blocks other operations.

    This is probably the fastest you'll get. You have to realize that issuing 9M updates on a single server is going to be a heavy operation. Let's say that you could get 3k updates / second, you're still talking about running for nearly an hour.

    And that's not really a "mongo problem", that's going to be a hardware limitation.

提交回复
热议问题