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?

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

1 Answer 1

-1
\$\begingroup\$

This is a lot faster:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.