未知的顶级运算符:$ orderby [英] unknown top level operator: $orderby

查看:109
本文介绍了未知的顶级运算符:$ orderby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 node.js + 猫鼬应用程序在服务器="https://www.digitalocean.com/" rel ="nofollow noreferrer"> DigitalOcean VPS.

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

看起来像这样:

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

会引发此错误:

未知的顶级运算符:$ orderby

unknown top level operator: $orderby

已经考虑的解决方案:

  • 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.

推荐答案

不再支持$orderBy之类的查询修饰符:

Query modifiers like $orderBy are no longer supported:

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

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

根据文档的建议,使用 sort limit:

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

这篇关于未知的顶级运算符:$ orderby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆