Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Faster way to remove all entries from mongodb collection by dropping collection and recreating schema

When I want to remove all objects from my mongoDB collection comments I do this with this command:

mongo $MONGODB_URI --eval 'db.comments.deleteMany({});'

However, this is super slow when there are millions of records inside the collection.

In a relational db like Postgres I'd simply copy the structure of the collection, create a comments2 collection, drop the comments collection, and rename comments2 to comments.

Is this possible to do in MongoDB as well? Or are there any other tricks to speed up the progress?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Thanks, the answers inspired my own solution. I forgot that MongoDB doesn't have a schema like a relationalDB.

So what I did is this:

1. dump an empty collection + the indexes of the collection

mongodump --host=127.0.0.1 --port=7001 --db=coral --collection=comments --query='{"id": "doesntexist"}'  --out=./dump

This will create a folder ./dump with the contents comments.bson (empty) and comments.metadata.json

2. Drop the comments collection

mongo mongodb://127.0.0.1:7001/coral --eval 'db.comments.drop();'

3. Import new data new_comments.json (different from comments.bson)

mongoimport --uri=mongodb://127.0.0.1:7001/coral --file=new_comments.json --collection comments --numInsertionWorkers 12

This is way faster than first adding the indexes, and then importing.

4. Add indexes back

mongorestore --uri=mongodb://127.0.0.1:7001/coral --dir dump/coral --nsInclude coral.comments --numInsertionWorkersPerCollection 12

Note that --numInsertionWorkers speeds up to process by dividing the work over 12 cpus. How many cpus do you have can be found on OSx with:

sysctl -n hw.ncpu
over 4 years ago · Santiago Trujillo Report

0

db.cities.aggregate([{ $match: {} }, { $out: "collection2" }]) in case you can login to the mongo prompt and simply drop the previous collection. Otherwise, the approach you have posted is the one.

mongoexport.exe /host: /port: /db:test /collection:collection1 /out:collection1.json mongoimport.exe /host: /port: /db:test /collection:collection2 /file:collection1.json

Thanks, Neha

over 4 years ago · Santiago Trujillo Report

0

For mongodb version >=4.0 you can do this via db.comments.renameCollection("comments2") ,but it is kind of resource intensive operation and for bigger collections better you do mongodump/mongorestore. So the best action steps are:

 mongodump -d x -c comments -out dump.bson
 >use x
 >db.comments.drop()
 mongorestore -d x -c comments2  dump.bson

Plese, note deleteMany({}) is even more resource intensive operation since it will create oplog single entry for every document you delete and propagate to all replicaSet members.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!