mongoose - 使用 mongoose 全文搜索和 KeystoneJS 分页

标签 mongoose pagination keystonejs

我有全文搜索,返回 Keystone 模型的结果。问题是我使用 model.find() 生成这些结果。我想对这些结果进行分页,但找不到将 $text.paginate() 一起使用的方法。有没有办法将 mongoose 全文搜索与 Keystone 分页集成,甚至对 model.find() 的结果进行分页?

编辑

我已经厌倦了使用 keystone.list('List').paginate().find() 但收到错误:“回调不是函数。”也许我的代码还有其他问题:

keystone.list('Document').paginate({
  page: req.query.page || 1,
  perPage: 10,
  maxPages: 6,
})
.find(
  { $text : { $search : req.query.search } },
  { score : { $meta: "textScore" } }
)
.sort({ score : { $meta : 'textScore' } })
.exec(function(err, results) {
  locals.documents = results;
  next();
});

最佳答案

这就是我设法做过滤器的方法。 Keystone boilerplate 。下面是我的待办事项模型

import keystone from 'keystone';

const { Field: { Types } } = keystone;

const Todo = new keystone.List('Todo');

Todo.add({
   title: {
   type: String, required: true, default: Date.now,
 },
 description: { type: Types.Html, wysiwyg: true },
 createdAt: { type: Date, default: Date.now },
});

Todo.relationship({ path: 'users', ref: 'User', refPath: 'todos' });
Todo.schema.index({ title: 'text' });
Todo.register();

export default Todo;

和分页过滤器

export const list = (req, res) => {
const search = new RegExp(req.query.search || '', 'i');
const query = {
  title: search,
};
Todo.paginate({
   page: req.query.page || 1,
   perPage: 5,
   maxPages: 10,
   filters: {
     $or: [query],
   },
 }).sort('-createdAt').exec((err, items) => {
 if (err) return res.status(500).json(err);

   return res.json({
     data: items,
   });
 });
};

关于mongoose - 使用 mongoose 全文搜索和 KeystoneJS 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46059600/

相关文章:

javascript - 基石JS : S3 image uploads being auto-renamed with temporary string

node.js - 安装 Keystone@next 4.0 beta 时遇到问题

node.js - 查询mongodb的条件条件

javascript - 使用 ajax 分页的 Google Rich Snippet

java - 在不更改原始方法签名的情况下对服务方法进行分页调用

ruby-on-rails - Rails 中的分页 - 在编辑/更新属于其他页面的元素后始终重定向到第一页

node.js - 查询 MongoDB 嵌套字段

mongodb - 想要更新 mongodb 中的二级数组对象

mongodb - 在 mongodb 中编辑子文档 N-N 关系

keystonejs - 如何在 KeystoneJS 中使用 "Content"字段类型?