error in script not seen when run in shell

Michael shared this problem 5 years ago
Solved

Hi

I'm relatively new with mongo and nosqlbooster. I've been doing a mongo university course on aggregation. One of the labs has the student put their aggregation pipeline through their provided javascript function to emit an altered result to submit (presumably to make the answer not so obvious).

The problem I had is when I ran their script together with my aggregation pipeline via nosqlbooster (Mac without mongo db or tools installed), I got an error: "Cannot read property 'pop' of undefined".

However when I run the same thing through command-line mongo shell (on PC), there's no problem (I see the correct answer emitted).

In looking at their js code, it appears that this line raises the issue.

let { _id, title, rated } = resultsExplain.stages.pop()["$project"]
Below is the code (I've trimmed the $match part). I'm not sure what else I can offer to help explain or clarify the error. I feel there is some difference between mongo's native command-line shell (shell version: 4.0.10 on Win10) and nosqlbooster (using 5.1.14 on Mac). Any tips appreciated. Thanks.

var pipeline = [
    {
        $match: {
            genres: { $nin: ['Crime', 'Horror'] },
            rated: { $in: ['G', 'PG'] }
        }
    },
    {
        $project: { _id: 0, rated: 1, title: 1 }
    }
]

var validateLab2 = pipeline => {
  let aggregations = db.getSiblingDB("aggregations")
  if (!pipeline) {
    print("var pipeline isn't properly set up!")
  } else {
    try {
      let resultsExplain = aggregations.movies.aggregate(pipeline, {
        explain: true
      })
      let result = aggregations.movies.aggregate(pipeline).toArray().length
      let data = 0
      while (result != 1) {
        data++
        result = result % 2 === 0 ? result / 2 : result * 3 + 1
      }
      let { _id, title, rated } = resultsExplain.stages.pop()["$project"]
      return title && rated && !_id
        ? print("Answer is", data)
        : print("Your $project stage doesn't seem correct")
    } catch (e) {
      print(e.message)
    }
  }
}

validateLab2(pipeline)

Replies (3)

photo
1

Thank you for your bug report. We will fix the explain result bug in the next update. In NoSQLBooster, the "aggregate" method always returns a cursor, while MongoShell 4.0 returns result in object directly when the option "explain" is true.

There is a work-around solution. Please replace the following statements.

let resultsExplain = aggregations.movies.aggregate(pipeline, {
        explain: true
      }).toArray()[0];  //add  toArray()[0]

photo
1

Resolved in 5.2.1

photo
1

Hi @qinghai,


Unfortunately there still seems to be major differences between mongosh and NoSQLBooster 7.1.12.

I want to retrieve an array of userIDs using an aggregate pipeline.

Here is what I do in NoSQLBooster

    const userPipeline = [{ $match: {...userQuery}}];
    const userIds = db.users.aggregate(userPipeline).map(u => u._id);
Unfortunately, this code doesn't work in mongosh. For it to work in mongosh, I need to do the following:

const userPipeline = [{ $match: {...userQuery}}];
const userIds = db.users.aggregate(userPipeline).map(u => u._id).toArray();
For me it is a big deal if the syntax in NoSQLBooster is not the same as the syntax in mongosh (which is the reference).


Are these kind of discrepancies documented anywhere?


Thanks!

Xavier

https://featherfinance.com


p.s. otherwise, I would really like NoSQLBooster to succeed -- I particularly love the Tree view. I know of no other tool that does this for mongo.

photo
2

NoSQLBoosterV7 'shell is based on mongo shell 5, not mongosh. Although MongoDB's own documentation says that mongosh will be compatible with legacy mongo shell's methods, in fact, some methods are very different, such as the map method. The mongosh returns cursor, while legacy mongo shell returns array data.

MongoDB does not have an official document listing all the differences.

It is very difficult for us to find a compatible way.

In short, the implementation of the mongo shell used by NoSQLBooster V7.

In the future, we will adopt the implementation of mongosh

photo
1

Thanks @qinghai, I appreciate your answer. I suspected something along those lines.

I completely understand your situation, I didn't like the way the MongoDB team did the migration from mongo to mongosh. It's very difficult to understand the differences, both of these shells are poorly documented. They don't give comprehensive error messages on failure, etc. etc. Hopefully, they will do a better job in the long run.


I'm happy to hear that you plan to migrate to mongosh. I am migrating to mongosh myself because MongoDB says it is the preferred shell (I presume they will discontinue support for the mongo shell eventually).


Thanks again for your innovative software!

photo
Leave a Comment
 
Attach a file