M121 Lab - $match validate command hangs

TBH, mongoshell did not have a straight forward install on Mac OS X, so maybe not expecting things to run perfect, but things seemed better since I used the software update fix. Anyhow…

I was able to spawn mongo shell fine, connect fine use the correct db fine, even though that doesn’t seem to be mentioned in the lab. There are only so many sample datasets, so eliminating seems easy enough.

Finishing up, it seems like the js load returns true, the pipeline I enter returns the dataset created by entering it, but once I try the validation step, validateLab1(pipeline), everything hangs.

One note, my pipeline is not using allowDiskUse, which might help, but I have my doubts.

Validate code looks interesting:

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

When you run the pipeline, (enter the var pipeline string) does it return anything?

It should just go back to the command prompt and not return anything. My experience with the shell is if it gives you a new line with a blinking cursor, the command wasn’t terminated correctly.

Also, I would recommend installing VS code for Mac and connecting to the cluster through that. You can run everything through VS code that way. (there is a terminal in VS Code)

I’m not totally certain from a glance how the objects are working, but it appears this just chews on numbers forever unless it has the right numbers. Willing to be wrong though, of course.

You are doing the chapter 1 homework lab right?

Damn it - it’s the Collatz Conjecture! I spent too long looking at that thing a couple years ago.

Well, the code is obviously broken somehow, but I could reverse engineer the math. Still, I’d prefer to have this working, and fully understand the example.

I mean, technically the answer is supposed to be provided with that javascript, but I could probably insert some print statements into that to see what it’s getting stuck on, or run the individual lines manually.

I’ll get there, I think.

Okay. here is what the cause is.
The prerequisites require a special connection to a database

mongo “mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0” --authenticationDatabase admin --ssl -u m121 -p aggregations --norc

, which is only ever mentioned in the prerequisites lecture and slide.

I was assuming that we were using a vanilla sample_mflix, which is part of the default data loaded from the sample data.

If you use that instead of the right database, the numbers are close but not correct.

And then, if you run the validate anyway, you get an infinite loop, because the Collatz Conjecture can’t use zero or negative numbers.

So “let aggregations = db.getSiblingDB(“aggregations”)” returns 0 because the datastore doesn’t exist and mongo never says anything is not there - it just assumes that the datasource is empty so far.

0 is even, and 0/2 = 0, so the loop runs forever.

I’m sure debugging this will be more fun than getting the right answer in this lab.

Oddly getting the pipeline counts remotely close with sample_mflix made me think I was on the right track. And I was able to create the right pipeline with a vanilla sample_mflix, even if testing didn’t work properly.