使用KeystoneJS paginate进行mongoose全文搜索
我有全文搜索,返回Keystone模型的结果。问题是我使用使用KeystoneJS paginate进行mongoose全文搜索,mongoose,pagination,keystonejs,Mongoose,Pagination,Keystonejs,我有全文搜索,返回Keystone模型的结果。问题是我使用model.find()生成这些结果。我想对这些结果进行分页,但找不到将$text与.paginate()一起使用的方法。是否有一种方法可以将mongoose全文搜索与Keystone分页集成,甚至可以对model.find()中的结果进行分页 编辑 我已经厌倦了使用keystone.list('list').paginate().find(),但我收到一个错误:“回调不是函数。”也许我的代码还有其他问题: keystone.list('
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.list('Document').paginate({
page: req.query.page || 1,
perPage: 10,
maxPages: 6,
filters: {
$text: {
$search: req.query.search || ''
},
},
optionalExpression: {
score: {$meta: "textScore"}
},
})
.sort({ score : { $meta : 'textScore' } })
.exec(function(err, results) {
locals.documents = results;
next();
});
paginate
函数将其传递到find
函数中 这就是我如何进行筛选的方法。下面是我的todo模型
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,
});
});
};
您是否尝试过model.paginate().find()?我尝试过model.paginate().find(),这会产生错误,因为paginate()不是model
的函数。我还尝试了paginate().model.find(),它的作用与我简单地调用model.find()的作用相同。请尝试以下操作:keystone.list('MyList')。paginate().find()-其中'MyList'是列表的名称。我有。我编辑了原始帖子,并在编辑时收到了错误+我的代码。你在哪里使用回调?