The error occurs because your script uses invalid or incorrect syntax for JavaScript, which is what the MongoDB shell uses.

Your quotation marks ( and ) in your code are smart quotes, not regular ASCII double quotes ("). MongoDB requires standard quotes, also the Ellipses (...): The ... is not valid in this context. These placeholders are not executable syntax and fields like roles, clientSource, serverAddress, and others cannot be empty or invalid.

Here’s the corrected version of the db.createUser() command:

db.createUser({
  user: "testUser",
  pwd: passwordPrompt(), // Prompt user for password
  customData: { info: "some info" },
  roles: [
    { role: "readWrite", db: "testDatabase" } // Example role
  ],
  authenticationRestrictions: [
    {
      clientSource: ["127.0.0.1"], // Example IP address
      serverAddress: ["127.0.0.1"]
    }
  ],
  mechanisms: ["SCRAM-SHA-256"], // Specify authentication mechanism
  passwordDigestor: "client" // Specify digestor
});
1 Like