Creating a new user on the admin database of a locally installed instance of MongoDb

Good afternoon,
I am attempting to create a new user on the admin database. I have opened the MongoDb shell and am attempting to run the below command and get a syntax error. I have copied the command exactly from the online documentation and selected the admin database in my MongoDb shell but get the error below. Can someone help me please understand why I am getting this error and how to fix it?

db.createUser( {
user: “testUser”,
pwd: passwordPrompt(), // Or “”
customData: { “some info” },
roles: [
{ role: “”, db: “” } | “”,

],
authenticationRestrictions: [
{
clientSource: [“” | “”, …],
serverAddress: [“” | “”, …]
},

],
mechanisms: [ “<SCRAM-SHA-1|SCRAM-SHA-256>”, … ],
passwordDigestor: “<server|client>”
})
SyntaxError: Unexpected token (4:28)

2 | user: “testUser”,
3 | pwd: passwordPrompt(), // Or “”

4 | customData: { “some info” },
| ^
5 | roles: [
6 | { role: “”, db: “” } | “”,
7 | …

Hi @Stephen_Fitzgerald and welcome to the community!

To get a properly formatted post, I recommend you read the following guide:
Formatting code and log snippets in posts - About the Community / Getting Started - MongoDB Developer Community Forums

Before talking about the problem you are experiencing, I strongly recommend that you take courses at mongodb university, because they will help you better understand how to use mongodb:

MongoDB Courses and Trainings | MongoDB University

Now to your problem, at the end of the documentation page there are examples that can help you create a user, you can’t just copy and paste generic user creation syntax. The reference to the examples in the documentation follows:

db.createUser() - MongoDB Manual v8.0

I hope everything will be useful to you.

Regards

2 Likes

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
});