Getting 'Unknown top level operator $or'

I have an app deployed on heroku, which keeps crashing because of this error:

 events.js:291
2020-12-09T16:08:01.009305+00:00 app[worker.1]:       throw er; // Unhandled 'error' event
2020-12-09T16:08:01.009305+00:00 app[worker.1]:       ^
2020-12-09T16:08:01.009306+00:00 app[worker.1]: 
2020-12-09T16:08:01.009306+00:00 app[worker.1]: MongoError: Unrecognized expression '$or:'

And here’s the implementation of my change stream:

const profPipeLine = [
        {
            '$match': {
                '$expr': {
                    '$or:': [
                        {'$eq': ['operationType', 'insert']},
                        {'$eq': ['operationType', 'update']},
                        {'$eq': ['operationType', 'delete']}
                    ]
                }
            }
        }

 const options = { 'fullDocument': 'updateLookup' };

    const profileStream = MatchProfile.watch(profPipeLine, options); 

    profileStream.on('change', next => {
...
})

Idk why I keep getting that error about $or, because it works without errors locally, and its not the first time i’ve used it. I’m also using nodejs event emmitters in my project, if that may have anything to do with it since I’m sure change streams use the same event emmitter library from nodejs.

Its probably the syntax error. Looks like the colon : after the $or ('$or:') is causing the error.

1 Like

If I may. I suggest that you look at https://docs.mongodb.com/manual/reference/operator/aggregation/in/ to replace the whole $expr. The following should be equivalent:

'$match' : { 'operationType' : { '$in' : [ 'insert', 'update', 'delete' ] } }

The $in operator usage is correct, but the link should be: $in — MongoDB Manual

Note that there is an $in operator and then there is a $in aggregation operator.

1 Like