I’m writing an init script (e.g. /docker-entrypoint-initdb.d/init.js
) which will be executed by the mongo
Docker image when the container is first created. (See the docs for the image, “Initializing a fresh instance” section.)
Within this script, I need to open a file from disk (on the container), and then insert an object into the database with that file’s content as a property. Something like this:
import fs from "fs";
let data = fs.readFileSync("/path/to/somefile.txt", { encoding: "base64" });
db.example.files.insertOne({
filename: "somefile.txt",
data: Binary.createFromBase64(data),
});
But this script is written for NodeJS — how can I do the same in a plain JS script executed via MongoDB directly (e.g. mongosh -f init.js
)?