mongodb - Mongo db Collection find 从客户端的第一个条目而不是最后一个条目返回
问题描述
我正在使用 mongodb,我正在使用一些工作正常的条件查询数据库,但结果来自第一个条目到最后一个条目,因为我想从最后添加的条目查询到数据库中的集合
TaggedMessages.find({taggedList:{$elemMatch:{tagName:tagObj.tagValue}}}).fetch()
解决方案
Meteor 使用和的自定义包装版本,Mongo.Collection
以Mongo.Cursor
支持开箱即用的反应性。它还抽象了 Mongo 查询 API,使其更易于使用。
这就是为什么从末端访问元素的本机方式在这里不起作用的原因。
在服务器上
为了$natural
正确使用 Meteor,您可以在服务器上使用该hint
属性作为选项(请参阅文档中的最后一个属性):
const selector = {
taggedList:{ $elemMatch:{ tagName:tagObj.tagValue } }
}
const options = {
hint: { $natural : -1 }
}
TaggedMessages.find(selector, options).fetch()
旁注:如果您需要访问“本机”Mongo 驱动程序,则需要使用rawCollection
在客户端
在客户端上,您无法真正访问 Mongo 驱动程序,而是访问看似相似的 API(称为minimongo
包)。那里你不会$natural
有空(可能在将来),所以你需要使用降序排序:
const selector = {
taggedList:{ $elemMatch:{ tagName:tagObj.tagValue } }
}
const options = {
sort: { createdAt: -1 }
}
TaggedMessages.find(selector, options).fetch()
推荐阅读
- api - 如果其中有附加路径,如何在未来的异步中编写 API
- css - 如何在 WordPress 的 Tabs Responsive 中使标签居中对齐?
- android - Android Studio:将格式文件保存到 repo
- python - 尝试在 python 打开文件函数中使用 filenotfound 错误
- php - PHP Guzzle 支持的最大“并发”请求数是多少?
- pandas - 高斯滚动重量熊猫
- php - 即使 chmod 设置为 0777,PHP fopen 文件也无法写入,Linux 服务器问题
- android - 如果 android:windowIsTranslucent 在 Android 11 上设置为 true,则 overridePendingTransition 不起作用
- swift - RxSwift - 如何调用定期返回驱动程序的方法?
- r - 如何为时间序列中的最后一个日期添加点或标记