未知的顶级运算符:$ orderby [英] unknown top level operator: $orderby
问题描述
我们有一个 node.js + 猫鼬应用程序在
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
已经考虑的解决方案:
- 猫鼬默认情况下使用$ set ,但是我们在查询中未使用该运算符
- 此解决方案考虑了聚合,但是我们需要使用它来查找功能
- 此解决方案似乎是针对数组字段的,但是我们需要针对非数组索引的解决方案
- 我们不混合运算符,在此解决方案中说
- 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.
推荐答案
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屋!