Invoke a JavaScript file within the MongoDB shell

Running a JS file directly from the Mongo shell using the query

mongo localhost:27017/west createSumCollection.js

has resulted in an error:

"uncaught exception: SyntaxError: unexpected token: identifier : @(shell):1:6"

The content of CreateSumCollection.js includes basic database usage and db.createCollection with a simple object id and another field.

What could be causing this issue? The same script runs perfectly when executed directly in the mongoshell.

Attempting to load("C:/mongodb/data/db/createSumCollection.js") failed due to the "use west" statement. Removing this statement allowed the load to work. I am curious about the difference between load and mongo localhost:27017/west createSumCollection.js and how to successfully run the JS file directly.

Answer №1

use is a handy command that can only be used in the interactive shell environment.

When working in the interactive shell (or using load), you can enter the following commands:

use my_database
db.collection.find()

To achieve the same result in a JavaScript file on the command line, you would need to use:

db.getSiblingDB("my_database").collection.find()

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

The DOM seems to be producing NaN when receiving user input

Currently, I am in the process of developing a depreciation calculator that utilizes user input fields to generate an alert with the resulting value. When I set the variables to predefined test values without incorporating user input, the calculator funct ...

What is the best way to make an input text the focus when an item on a dropdown menu is clicked?

I have a website with a dropdown menu and an input box. I want the user experience to be more seamless by automatically focusing the mouse cursor inside the input box when they click on an option in the dropdown menu. This way, users can start typing right ...

Angular $http not triggering

I am just starting to learn about angular js. In the project I am currently working on, I have created a code snippet for authorization in a directive. However, when I try to call the validateUser function, the $http post call does not seem to be executing ...

Access PHP variables in JavaScript

In my project, I have a file named english.php which holds various variable values stored in the $LANG array. For example: $LANG['value_1']="abc"; $LANG['value_2']="xyz"; In addition to numerous .php files that include require_once( ...

In AngularJS, the $http get method is displaying the status code of the data object instead of

After pondering over this issue for several days, I am still unable to pinpoint what I am doing wrong. Any suggestions or insights would be greatly appreciated. My challenge lies in trying to showcase the response from a rest service to the user by utilizi ...

Looking for a vertical scrollbar solution for your idangerous vertical swiper?

Incorporating idangerous vertical swiper with two slides in a mobile app through jquery mobile has proven challenging. Specifically, I am struggling to implement a vertical scroll bar in the second slide due to its fixed height and the potential collision ...

endless cycle when utilizing useEffect with a mandatory function

I'm currently in the process of creating a custom hook for sorting and filtering tables within a dashboard application. However, I am encountering an issue with an infinite loop when attempting to refetch data after changing sort or filter fields. The ...

To view the previous or next image, simply click on the links and watch as the new image fades in seamlessly while maintaining

Whenever I click on a prev/next link, a specific div loads different images that loop. The JavaScript successfully changes the image source when the prev or next button is clicked. It works flawlessly. However, I am facing an issue. I want each new image ...

Add multiple entries if the key-value pair does not already exist

My current approach involves utilizing insertMany to add documents obtained from the spotify api. The structure of these documents is as follows: { spotify_id: "6400dnyeDyD2mIFHfkwHXN", name: "Pablo Honey", ... } This insertion process is achieved ...

Populate vue-multiselect with links from the router

Is there a way to populate the vue-multiselect dropdown with <router-link> options? Typically, router links are defined declaratively in the <template>, but vue-multiselect relies on binding an array of options. Any suggestions on how to approa ...

Enhance your live table previews with the power of React JS

I'm trying to optimize the process of live updating a table in React. Currently, I am using setInterval which sends unnecessary requests to the server. Is there a more efficient way to achieve this without overwhelming the server with these unnecessar ...

Ways to resolve the error "Uncaught TypeError: data.map is not a function"

Currently developing an app using reactJS and encountering the following error in the console when using the map function: TypeError: data.map is not a function. Despite successful API data calling as confirmed by console.log, the error persists when tryin ...

Dynamically changing the date format within the item-text of v-autocomplete in Vuetify

My current v-autocomplete component is fetching an array of items from GrowthTasks.edges to display. <v-autocomplete label="Start Week" :items="GrowthTasks.edges" item-text="node.growthStartDate" item-value="node.grow ...

Investigating and solving issues in Javascript and JQuery

Let me walk you through the JavaScript process I am working on: When a text is entered in the input bar and submitted, the script dynamically creates a new "div" element and places the text inside it. This new div is then appended to an existing div calle ...

What is the best way to utilize URL parameters to dynamically update a component's state within a class component

I am currently working on a React component that is making calls to two different API URLs, and the fetch operations are functioning as expected. However, I would like to utilize the URL handle structure, which looks like localhost://site/coins/[handle], a ...

Is it possible to establish a delay for requestAnimationFrame()?

My webpage has a very long layout with text in one column and a floating element in another. I want the floating element to follow the scroll of the window and return to its original offset once scrolling stops. The current code I have is: var ticking = ...

Tips on customizing label colors on a Donutchart using Google API

https://i.sstatic.net/Rpor0.png I am looking to change the color of 12.5 (to Green) and 25 (to red) based on the donut arc color in Google Charts. Below is the code snippet: var container = document.getElementById('chart_div'); var chart = new ...

The app crashed unexpectedly with an error code of MODULE_NOT_FOUND and no requirestack

While attempting to run nodemon with the command "run start" on macOS, I encountered the following error message. Unsure if the operating system is a factor. ERROR node:internal/modules/cjs/loader:926 throw err; ^ Error: Cannot find module '/User ...

Obtain a range of dates within a specified timeframe by utilizing JavaScript

Is there a way in JavaScript to retrieve a list of days between two dates in MySQL format without using any libraries? Here is an example of how it can be done: function generateDateList(from, to) { var getDate = function(date) { //MySQL Format ...

concealing the upper header while scrolling and shifting the primary header upwards

Is there a way to use CSS to move the main header navigation, including the logo and links, on my website up when scrolling down in order to hide the top black bar header that contains contact information? The website in question is atm.truenorthmediasol ...