unknown top level operator: $orderby

送分小仙女□ 提交于 2019-12-11 13:42:32

问题


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:

  • less than / equal to
  • limit
  • order by

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:

  • Mongoose uses $set by default, however we're not using that operator in our query
  • This solution considers aggregation, however we need it for find function
  • This solution seems to be for array fields, however we need solution for non-array indexes
  • We're not mixing operators, as said in this solution

Need inputs on how to fix this.


回答1:


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);



回答2:


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
    }
})


来源:https://stackoverflow.com/questions/48049038/unknown-top-level-operator-orderby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!