Chapter 1 : cannot execute the validateLab1.js script

Hi,

I create a script (script.js) with my pipeline and has counter = 23

var pipeline = [.....]
var counter = db.movies.aggregate(pipeline).itcount()
print(counter)

I run it in my mongo shell with this command : load(“script.js”)

Then I try to launch validateLab1.js downloaded in mongo website.
Then, in mongo shell I run : load(“validateLab1.js”)

And I has this error :

MongoDB Enterprise Cluster0-shard-0:PRIMARY> load("validateLab1.js");
2019-01-14T17:08:37.646+0100 E QUERY    [thread1] SyntaxError: missing ; before 
statement :
@(shell):1:1

2019-01-14T17:08:37.646+0100 E QUERY    [thread1] Error: error loading js file: 
validateLab1.js :
@(shell):1:1

If someone has a solution :wink:

When using load() your .js must respect pure javascript syntax.

For example, each statement must end with a semicolon. In your code sample you have none.

Also you cannot use mongo shortcuts like ‘use DatabaseName’ your must do db = db.getSisterDB( ‘DatabaseName’ ) ;

Thx for your reply.
In the file validateLab1.js downloaded on the mongo website there is no semicolon.

My first script runs without any error.

var pipeline = [.....]
var counter = db.movies.aggregate(pipeline).itcount()
print(counter)

Funny.

I don’t know then.

I used to get the same error and then I would edit my .js to add a semicolon and the script would load correctly.

I just when back to an old script and remove the semicolon and it works. I could also load validateLab1.js which has no semicolon indeed. I starting to suspect a windows to linux character conversion issue.

We are getting closer. I have a failed script with ‘use DB’ and replaced it with db = db.getSisterDB( ‘DB’ ) without adding semicolon and it worked. So calling use is a real problem. Leaving semicolons out is not. I keep looking.

I use Linux for information.
My Mongo version is 3.2.20

The m5sum of my validateLab1.js is 6397e8096480cc273c40a86adfaa9423. Could you check you have the same? If not can you sent yours to me with https://www.dropbox.com/request/ZbO2VgKSe1osne9pcADl

You might have accidentally changed the code of validateLab1.js file. Try by downloading the file again.

Still not sure why, but doing a brute force troubleshooting proved that there was a conflict with the “let” command. I removed all “let” commands from variables declarations and I was able to load and run the script successfully.

I’m using Ubuntu 16.04 and mongo CLI 3.2.19

Please let me know if this worked @Damien_63388

MongoDB Enterprise Cluster0-shard-0:PRIMARY> load('validateLab1.js')
true
MongoDB Enterprise Cluster0-shard-0:PRIMARY> validateLab1
pipeline => {
  aggregations = db.getSiblingDB("aggregations")
  if (!pipeline) {
    print("var pipeline isn't properly set up!")
  } else {
    try {
      var result = aggregations.movies.aggregate(pipeline).toArray().length;
      sentinel = result;
      data = 0
      while (result != 1) {
        data++
        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) {
      print(e.message)
    }
  }
}
MongoDB Enterprise Cluster0-shard-0:PRIMARY> validateLab1(pipeline)
Answer is xx
MongoDB Enterprise Cluster0-shard-0:PRIMARY>

You need to pass your pipeline as parameter.

Like validateLab1(pipeline)

give the complete path of validateLab1.js file and check once
You can use it in below way-
load(‘C:\Program Files\MongoDB\Server\4.0\bin\validateLab1.js’)

Yes like @Avirup_Dutta_Gupta_92787 already mentioned above, your error simply says that the load command can’t find this file validateLab1.js in the current directory where you run this command.
So for example (let’s assume I have extracted this file to my path: myHomeFolder/Downloads) in this case I need to use this command
load("./Downloads/validateLab1.js");

Update: after upgrading my mongo CLI to the latest (4.0.5) all errors while loading validateLab1.js and validatedLab2.js went away. It seems like the Javascript interpreter for 3.2 was not current enough to parse the scripts for the labs.

1 Like

Hi all,
Sorry I was sick and I do not take time to look at your answers.

I update my Mongo CLI to 4.0.5 and all work fine :slight_smile:

Thx all for your answers.