1
\$\begingroup\$

This code appends a boolean column to all documents in the birds collection.

db.birds
 .find()
 .snapshot()
 .forEach(function(el) {
 el.hi = true
 db.birds.save(el)
 })

Here's how the command is run in production.

mongo db_stuff/db_name -u username -p password theScript.js

This command runs very slowly. Should I look into the $set operator or something else that will speed this up?

asked Feb 19, 2018 at 20:36
\$\endgroup\$
1
  • \$\begingroup\$ What does your db look like? Size? Columns? Types? A schema is usually required when asking about db optimisations. \$\endgroup\$ Commented Feb 21, 2018 at 9:48

1 Answer 1

-1
\$\begingroup\$

This is a lot faster:

db. birds.updateMany(
 {},
 { $set:
 {
 hi: true
 }
 }
)
answered Feb 19, 2018 at 22:44
\$\endgroup\$
2
  • 5
    \$\begingroup\$ That's great! But this is not a review :-( \$\endgroup\$ Commented Feb 19, 2018 at 22:47
  • \$\begingroup\$ This is definitely the way to go. \$\endgroup\$ Commented Feb 20, 2018 at 4:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.