Promises or async/await in mongosh?

495 Views Asked by At

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?

1

There are 1 best solutions below

0
SaroVin On

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