5

I have a mongosh script that should execute things synchronously. Can I use promises or async/await in mongosh? It seems like I can't. Is there a way to ensure things don't get executed out of order?

For example

(1) db.clients.find({}).forEach((client) => {
  db.addresses.insertMany([
    { ....
    }

(2) db.addresses.find({}).forEach...

2 gets executed by the mongosh when 1 is still looping. Any thoughts?

cierech
  • 383
  • 4
  • 14
  • It could be possible inside a constructor (or property, which is not explicitly mentioned). See [Limitations on Database Calls](https://www.mongodb.com/docs/mongodb-shell/reference/compatibility/#limitations-on-database-calls) – Wernfried Domscheit Sep 28 '22 at 15:24

1 Answers1

0

You can use two separate scripts, so be sure to finish the first before running the second.

mongosh $MONGODB_URI queries/first.js queries/second.js
SaroVin
  • 1,583
  • 3
  • 23
  • 46