Mongoerror: Unknown top level Operator: $expr

Asked

Viewed 180 times

0

I’m trying to do this in my code and works perfectly on the localhost but on the Umbler server is giving error.

FOLLOWS THE ERROR:

MongoError: unknown top level operator: $expr

Follow the code I’m trying to do at Nodejs with Mongosse:

await model('UsoApi').find({
    $and: [{
      $or: [
        { userIdDaChave: body.userIdDaChave },
        { chaveUsada: body.chaveUsada }
      ]
    },
    {
      "$expr": {
        "$and": [
          { "$eq": [{ "$year": "$createdAt" }, mesAno[1]] },
          { "$eq": [{ "$month": "$createdAt" }, mesAno[0]] }
        ]
      }
    }]
  }).sort({ createdAt: 'desc' });

Someone can help me?

1 answer

1

The most important thing in this type of question is to specify all the differences between the environments. If it works in one place and does not work in another, the most likely is that there is some difference between environments.

In this case, as the operator $expr is only available as of version 3.6 of Mongo, it is important to check if the version of Mongo DB on the server is higher.

Alternatively, to resolve your query, you could try using the between between 2 dates, specifying the desired year and month with day 1 as the minimum and (year and month) + 1 month and day 1 as the exclusive maximum.

{
  $and: [
    {
      $or: [
        { userIdDaChave: body.userIdDaChave },
        { chaveUsada: body.chaveUsada }
      ]
    },
    {
      createdAt: {
        $gte: DATA_INICIAL,
        $lt: DATA_FINAL
    }
  ]
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.