Lab: Using Cursor-like Stages Compass Issue

I have been trying to use compass due to your recommendation. It is great for debugging an aggregation. However it seems to be failing with the following:

// This works in the shell
    db.movies.aggregate([
      {
    $match: {
      countries: 'USA',
      'tomatoes.viewer.rating': { $gte: 3 },
      $expr: { $isArray: '$cast' }
    }
  },
  { $limit: 1 }
])

// But this fails in compass for the $expr
{
  countries: 'USA',
  'tomatoes.viewer.rating': { $gte: 3 },
  $expr: { $isArray: '$cast' }
}

What am I doing wrong in Compass?

Please follow forum guidelines and do not post code that is possible answers.

What is the error message?

Expected ‘[’ or AggregationStage but found {

The error occurs in compass. By the way this works:

{
countries: ‘USA’,
‘tomatoes.viewer.rating’: { $gte: 3 },
$expr: true
}

But of course does not do what I want.

Rob

According to

https://docs.mongodb.com/manual/reference/operator/aggregation/isArray/

the syntax should be

{ ‘$isArray’ : [ ‘$cast’ ] }

The fact this it works in the shell is unknown to me. However I suspect that if $cast is indeed an array the syntax is valid but the result is wrong.

The square brackets also fails for me in compass. Does it pass for you?

The fail is not an incorrect match. Compass rejects the code with the above error message.

Rob

Also, the documentation does include examples with my original syntax (without square brackets).

Rob

I will check somebody before the end of the day.

Can you share the Atlas connection string you are using to connect in Compass?

The one I have, I took the course in december, don’t seem to work.

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

@robkat

$isArray returns a boolean value, not a document. What are you trying to do with that? Notice that the $isArray documentation here shows the following example:

db.warehouses.aggregate([
   { $project:
      { items:
          { $cond:
            {
              if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] },
              then: { $concatArrays: [ "$instock", "$ordered" ] },
              else: "One or more fields is not an array."
            }
          }
      }
   }
])

Compass is much less forgiving of syntax errors than the shell and your example I think has some problems. Think about this and try again.

I am trying to match documents where cast is an array. I am using the $expr operator to do that. It is far better to throw out documents that do not have cast as an array than to pass them all on to the followng stages.

https://docs.mongodb.com/manual/reference/operator/query/expr/#op._S_expr

It is not a syntax error since this works:

$expr: true

It is not a syntax error since it works in the shell.

Why should this be a syntax error anyway?

Rob

A better way (since it works :grinning:) to accomplish what I want:

cast: {$elemMatch: {$ne: null }}

I am going to avoid using $expr since it has inconsistencies between the shell and compass. Therefore it might also not work in the drivers. Is $expr a new feature?

Rob