How to run query in Shell after loading the JS

Hi, I have loaded the Javascript file in shell using the load command and it returned the response as TRUE

Once it is done, I would like to execute the query in the script file but not sure how to do it. Any help is much appreciated.

hey @Manikandan_67591

Do you mean that you have run


and want to run?


Or have you made your own script that you want to execute?

Below is the contents of the file I refered to

var validateLab1 = pipeline => {
  let aggregations = db.getSiblingDB("aggregations")
  if (!pipeline) {
    print("var pipeline isn't properly set up!")
  } else {
    try {
      var result = aggregations.movies.aggregate(pipeline).toArray().length
      let sentinel = result
      let data = 0
      while (result != 1) {
        result = result % 2 === 0 ? result / 2 : result * 3 + 1
      if (sentinel === 23) {
        print("Answer is", data)
      } else {
        print("You aren't returning the correct number of documents")
    } catch (e) {

As you can see, as long as you assign your function to a variable, once loaded in the shell you can call it directlt

Hi, I tried loading the js file using load(‘aggregate_structure.js’) in the shell prompt and it gave TRUE as response.
After that, when i typed aggregate_structure, it says “reference error: aggregation_structure is not defined”
Please help

The validateLab1.js file contains a function with the same name validateLab1. So when we load this file we are able to access the function validateLab1.

In the lab question, both the file and the function have same name. Do not be confused. File name does not matter, the function name does.

In your case, your file probably does not have a function name aggregate_structure. Please check the content of your file.

1 Like