MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Newb -- Administration via nodejs

Usually the examples and advice about administration say to use the mongo shell. Is it possible to do all necessary admin operations via the nodejs driver instead (no Mongoose)? I thought I wanted to use that route because I am going to be programming in that environment anyway and could cut down on the count of environments I have to learn by avoiding the shell.

I have mongodb installed on my VM and it works for a "nodebb" database with a "nodebb" user. I created an additional database "archive" with a user "archive". While authenticated as a database server administrator, I submit against the database named "archive", a command to update the password of the user named "archive". A message comes back saying user archive@admin is not found. But "admin" is the wrong database for the server to be looking for the user in, because I submitted the command against database "archive".

typeof (tmp.db = tmp.mongo_client.db("archive"));

tmp.admin_cmd = {
  updateUser: "archive",
  pwd: "tmp.pw"
};

with (tmp) ( async function () {
  try {
    await db.executeDbAdminCommand(admin_cmd);
    console.log("Submitted command.");
  } catch (err) {
    console.error(err)
  }
})();

MongoError: User archive@admin not found

Update: I did changeUserPassword through the mongo shell and it ran without error (addressing the same user and database).

Yes, you can do all the admin stuff using node. That’s what the mongo shell is, a node REPL interpreter with some very clever predefines.

1 Like

Hi @Jack_WAUGH,

The general answer is that all actions that can be performed via the mongo shell can also be actioned via a driver, since ultimately the shell is just sending database commands to your MongoDB deployment.

However, the shell provides helper functions with convenience methods and validation for administrative commands that may not have helpers in the standard driver API. Many administrative commands are run rarely during the lifetime of a deployment (for example, a replica set is only initialised once) or are typically invoked by an administrator rather than a developer or application.

You can write equivalent functions in your preferred driver language, but I would recommend using an admin shell or tool for convenient (and confident) updates.

For example, creating or updating replica set configurations involves manipulating a configuration document. You can view the implementation for mongo shell helpers such as Replication Methods by invoking a helper function without the parentheses. For example: try running rs.add<enter> in the mongo shell. You can also find this source on GitHub if you’re curious: rs.add().

The classic mongo shell is a custom REPL which embeds a JavaScript interpreter, but is not based on Node.

There’s also a new MongoDB Shell (aka mongosh) which was introduced in June of this year. This one is a Node REPL, as mentioned by @Jack_Woehr. The new shell (currently available as a Beta release) is designed to be embeddable, extensible, and more developer friendly than the classic shell. This shell has already been embedded into MongoDB Compass, MongoDB for VS Code, and other tools including JetBrains’ IDEs for a more streamlined development experience.

If you want to automate administrative actions for deployments, tooling like MongoDB Cloud/Ops Manager provides more convenient APis and enables integrations like the MongoDB Enterprise Kubernetes Operator.

I think the best approach for your “one environment” goal would be to use an IDE with MongoDB Shell integration (such as VS Code or one of the Jetbrains IDEs). However, if learning shell commands seems daunting or tedious, you may find a GUI like MongoDB Compass a better fit for your admin needs.

Regards,
Stennie

1 Like

I seem to have gotten around my immediate problem of not being able to double check the password for this user, but it’s still puzzling that the updateUser didn’t work.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.