2

We've a node.js + mongoose app running on Ubuntu linux servers spun on DigitalOcean VPS.

One of our mongoose queries have a text index with the following operators:

and looks like this:

db.sampleCollection.find({
  $text: {
    $search: userInput
  },
  $lte: {
    expired: (new Date()).addDays(30)
  },
  $limit: 9,
  $orderby: {
    postedAt: -1
  }
})

which throws this error:

unknown top level operator: $orderby

Solutions already considered:

Need inputs on how to fix this.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219

2 Answers2

3

Query modifiers like $orderBy are no longer supported:

Starting in v3.2, the query “meta” operators are deprecated in the mongo shell. In the mongo shell, use the cursor methods instead.

As suggested by the docs, use the cursor methods like sort and limit instead:

db.sampleCollection.find({
  $text: {
    $search: userInput
  },
  $lte: {
    expired: (new Date()).addDays(30)
  }
})
.sort({
    postedAt: -1
})
.limit(9);
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
-1

Check your mongodb version orderby has been deprecated since v3.2

$orderby Deprecated in the mongo Shell since v3.2
In the mongo shell,use cursor.sort() instead.

https://docs.mongodb.com/manual/reference/operator/meta/orderby/

db.sampleCollection.find({
    $text   : {
        $search: userInput
    },
    $lte    : {
        expired: (new Date()).addDays(30)
    },
    $limit  : 9,
    $sort: {
        postedAt: -1
    }
})
Ratan Kumar
  • 1,640
  • 3
  • 25
  • 52