The admin functionality in MongoDB is currently malfunctioning

I am attempting to pass parameters from a shell script to a MongoDB JavaScript file.

My current approach is as follows:

mongo --eval "var ADMIN_USER_NAME='$ADMIN_NAME', ADMIN_USER_PASSWORD='$ADMIN_PASSWORD'" insertusers.js

The contents of insertusers.js are:

cat insertusers.js

use admin

db.addUser( { user: ADMIN_USER_NAME,
          pwd: ADMIN_USER_PASSWORD,
          roles: [ "userAdminAnyDatabase" ] } )

Unfortunately, when I run the command, I encounter the following error:

mongo --eval "var ADMIN_USER_NAME='$ADMIN_NAME', ADMIN_USER_PASSWORD='$ADMIN_PASSWORD'"     insertusers.js
MongoDB shell version: 2.4.9
connecting to: test
Fri Apr 11 21:30:34.791 SyntaxError: Unexpected identifier at insertusers.js:5
failed to load: insertusers.js

I'm puzzled by this failure. The same functionality works when executed in a JavaScript shell.

Upon reaching the line use admin, an error

SyntaxError: Unexpected identifier
occurs.

Answer №1

If you're encountering issues, the culprit is probably the "use admin" command which requires adjusting certain user space variables.

Give the following solution a shot:

db.getSiblingDB("admin").addUser( { user: ADMIN_USER_NAME,
      pwd: ADMIN_USER_PASSWORD,
      roles: [ "userAdminAnyDatabase" ] } )

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Gulp for optimizing JavaScript files for web browsers, including import statements

Creating Isomorphic React Components I am looking to transpile my React components server-side into a single bundle.min.js file. However, I am encountering an issue where the import statements are not being resolved in the resulting file. The specific fi ...

Using React hooks to implement drag and drop functionality

As a backend engineer primarily, I've been struggling to implement a simple drag and drop feature for a slider I'm creating in React. Here's the behavior without using debounce: no-debounce And here's the behavior with debounce: with- ...

Retrieve information from JSON by utilizing query strings

Creating an API with an endpoint structured as day=1&time=1000. Using these query parameters to construct a mongo query that operates on JSON data: "_id" : ObjectId("62257ddd76b35400010e7015"), "applyOffersOn&qu ...

Adjust the vertical alignment of an HTML element

I've been trying to make an image move using the variable counter, but for some reason, it's not working. If I set document.getElementById("movee").style.top directly to a number, it works fine. Combining it with the counter variable should theor ...

Exploring the dynamic data loading feature in Vue 3 by fetching data from the server and displaying it using a v-for

I am encountering an issue where I want to display data dynamically from a database using a v-for loop. However, when I attempt to push new data into the array, they show correctly in a console.log() but do not reflect any changes in the template. I have ...

Having difficulty incorporating external SASS styles into my React.js project

In most of my projects, I follow a similar process for importing SASS styles. First, I create my react app and then install SASS using the command npm install node-sass. Next, I import my SASS file into my app components as shown below: import React from & ...

Navigating between pages using React and Material UI

My concept involves a <LandingPage> with a <Top> element that can be modified by user interaction. The <Top> component contains a pageState, which can be changed by clicking buttons on the First and Second pages. Is this considered good ...

Editing in real time, yet no instance to be found

I have developed my own custom non-jQuery ajax solution for building web applications. Recently, I encountered compatibility issues with IE9 while using TinyMCE, so I am considering switching to CKeditor. The editable content is contained within a div str ...

Obtaining localStream for muting microphone during SIP.js call reception

My approach to muting the microphone involves using a mediastream obtained through the sessionDescriptionHandler's userMedia event. session.sessionDescriptionHandler.on('userMedia', onUserMediaObtained.bind(this)) function onUserMediaObta ...

JavaScript reference problem when copying

Initially, I create a raw array by using the following initialization: $scope.rawUsers = angular.copy($scope.users); Subsequently, I make modifications to the data like so: function filterUsers(searchString, onlyMine) { $scope.users = []; _.find($scop ...

Changing the event when a class is active in Vue3

Question I am looking for a way to trigger an event when the 'active' class is added to an element. How can I achieve this? I believe this could potentially be accomplished using a watcher method, but I am unsure how to watch for the applicatio ...

Using React to Dynamically Display JSON Data as HTML

Struggling to incorporate HTML rendering from JSON data into my React component without relying on dangerouslySetInnerHTML. I want to include other React components within the rendered HTML, but facing challenges when trying to render multiple elements or ...

Creating Global Variables in Node/Express Following a Post/Get Request

How can I dynamically pass the payment ID received after a POST request to a subsequent GET request in Node/Express for a payment system? Below is an example code snippet from my project: let paymentId; app.post("/api/payments", (req, res) => ...

"Ionic application encountering issue with retrieving data from email inbox, resulting in undefined

I encountered an issue with creating a user account using Ionic Framework and Firebase. Oddly, the email box returns 'undefined' while the password box functions correctly despite being coded in a similar manner. Below is my HTML snippet: <io ...

extracting information from a database based on specific search criteria

"_id":{"$id":"61b5eb36029b48135465e766"}, "name":"push-ups","link":"https://google.com", "image":"https://google.com", "gender":["0","1" ...

VueJS Vuetify automatically centers default content

Vue cli version @ 5.0.6 | Vuetify version: [email protected] I have been utilizing Vue.js and Vuetify for some time now, and I can't shake the feeling that not all Vue.js/Vuetify components default to centered alignment. I recently initialized a ...

What is preventing me from accessing a dialog form modal through a button using JavaScript with jQuery?

There is jQuery and Bootstrap code on the same page, let's call it index.html This is my unique content <button id="btncreate" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Create</button> I am having trouble acces ...

Error: Attempting to access the 'products' property of an undefined variable leads to a TypeError

When running the build in my NEXT.js project, I encountered this error message: TypeError: Cannot read property 'products' of undefined at ClosetSpotlightItems (E:\AAAAAA\Susty\Z\Forked for Deploy\susty-next-fronted-fork ...

The filter function results in a blank array being returned

I'm facing a dilemma, I have a constant defined as export const STATUS = [{ 0: 'Draft', 1: 'Sent', 2: 'Processing', 9: 'Processed', 3: 'Scheduled', 4: 'Filed', 5: 'Suspende ...

Design a HTML-CSS layout to center your entire website and create a fixed menu

Created a website in Photoshop and sliced it for the web...currently working on CSS styling. Need to center the entire site, as it's currently aligned to the left side of the screen. Want to make the menu freeze or hover as you scroll down the p ...