我们有一个 node.js + mongoose应用程序在 Ubuntu linux 上运行服务器运行 DigitalOcean虚拟专用服务器。
我们的一个 Mongoose 查询有一个 text index使用以下运算符:
看起来像这样:
db.sampleCollection.find({
$text: {
$search: userInput
},
$lte: {
expired: (new Date()).addDays(30)
},
$limit: 9,
$orderby: {
postedAt: -1
}
})
抛出这个错误:
unknown top level operator: $orderby
已经考虑过的解决方案:
- > Mongoose uses $set by default ,但是我们没有在查询中使用该运算符
- > This solution considers aggregation , 但是我们需要它来实现查找功能
- > This solution seems to be for array fields , 但是我们需要非数组索引的解决方案
- 我们不混合运营商,as said in this solution
需要有关如何解决此问题的意见。
最佳答案
Query modifiers像 $orderBy
不再被支持:
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
代替:
db.sampleCollection.find({
$text: {
$search: userInput
},
$lte: {
expired: (new Date()).addDays(30)
}
})
.sort({
postedAt: -1
})
.limit(9);
关于javascript - 未知的顶级运营商 : $orderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48049038/