mongodb - GraphQL MongoDB:实现基于光标的 $text $search 分页
问题描述
在普通的光标分页中,我会根据created
字段对文档进行过滤和排序,这非常简单。像这样:
// `cursor` is a string
const books = await this.BooksModel.find({
...(cursor ? { created: { $lt: new Date(cursor) } } : null),
})
.sort({ created: -1 })
.limit(first)
同时,在实现搜索时,我(我想我)必须根据textScore
可能有重复的元数据对文档进行排序和过滤。为了防止这种情况,我需要另一个字段来过滤,即_id
. 我想它会是这样的:
// `cursor` is { id: string, score: number }
const books = await this.BooksModel.aggregate(
[
{ $match: { _id: { $ne: ObjectId(cursor.id) }, $text: { $search: 'sed' } } },
{ $project: { title: 1, score: { $meta: 'textScore' } } },
{ $match: { score: { $lt: cursor.score } } }
]
)
为了实现上面的示例,我需要光标不仅包含文档_id
(如普通分页),还包含score
. 我想光标的长度(解码)与正常的相比会很长。此外,光标现在包含一个对象而不仅仅是字符串,这对于像我这样的菜鸟来说是很奇怪的。这真的是构建基于光标的搜索分页的方法吗?
解决方案
推荐阅读
- r - 在 R 中填充时间序列时如何避免重复值?
- node.js - 如何使用环回休息连接器形成 URL?
- c# - 使用 ItemTemplating 更改网格的内容
- mongodb - 聚合 MongoDB 中的数组
- python - Python写多项式一行代码
- python - Django 模板:在 for 循环中解压需要 2 个值;有很多
- javascript - 当使用延迟加载 Angular 6 时,ngrx 的最佳文件夹结构是什么?
- spring-boot - 网关和聚合器 - CorrelationStrategy 错误
- linux - 从硬件 IRQ 号获取 Linux IRQ 号
- asp.net-mvc - 将 Angular 路由与 MVC 路由连接起来