【问题标题】:unknown top level operator: $orderby未知的顶级运算符:$orderby
【发布时间】:2018-06-11 10:55:02
【问题描述】:

我们有一个node.js + mongoose 应用程序在Ubuntu linux 服务器上运行,该服务器在DigitalOcean VPS 上运行。

我们的一个 mongoose 查询有一个 text index 和以下运算符:

看起来像这样:

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

引发此错误:

未知的顶级运算符:$orderby

已经考虑过的解决方案:

需要有关如何解决此问题的意见。

【问题讨论】:

  • @RolandStarke 这个查询无论如何都没有在 mongo shell 上运行,我们的是 MongoDB 服务器版本:3.4.3
  • 你试过$sort而不是$orderby吗?看看这个解决方案:stackoverflow.com/questions/5825520 它可能会对你有所帮助。
  • @PierreR-A 感谢您的投入,将尽快实施并回复

标签: javascript node.js mongodb mongoose full-text-indexing


【解决方案1】:

检查您的 mongodb 版本 orderby 自 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
    }
})

【讨论】:

    【解决方案2】:

    不再支持Query modifiers$orderBy 一样:

    从 v3.2 开始,查询“元”运算符在 mongo shell 中已弃用。在 mongo shell 中,请改用游标方法。

    按照文档的建议,请改用 sortlimit 等游标方法:

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

    【讨论】:

    • 使用mongodb 库从javascript 查询怎么样?
    猜你喜欢
    • 2015-10-15
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2015-01-01
    • 2021-03-12
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多