Run scripts against mongodb from a "javascript query"

Hey There

I want to to run a custom script in my Retool app to update my MongoDB collections. In order to do that I created a javascript query. The script have two `.updateOne' statements:

import { MongoClient, ObjectID } from "mongodb";
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
    const db = client.db("sample_mflix");
    const newId = ObjectID()
    db.collection("coll_tiledb_arrays_metadata_new").updateOne(
        {"_id":ObjectID( {{tiledbArraysTable.selectedRow.id}} )},
        {"$set": {"parent_ref": newId}}
    )
    db.collection("coll_modified_gses").updateOne(
        {"_id": ObjectID({{ModifiedGsesTable.selectedRow._idStudy}}), "Datasets.DatasetID": {{ModifiedGsesTable.selectedRow._idDataset}} },
        {"$set": {"Datasets.$._id": newId}}
    )
}
run().catch(console.dir);

I chose a javascript query because I wanted to use the same dynamiclly generated newId
for both update operations.

Is there a better way to do this instead of using a javascript query.

If not, how to:

  1. Dynamically get the connection string to my mongodb resource.
  2. Import the mongodb library.

Best,
Roy A