我目前有以下查询来返回给定status
的count
最近更新的帖子。
var query = Post.find()
.where('status').equals(status)
.sort('-updated')
.limit(count);
如果status
是"批准的",而count
是3,那么我的结果会是这样的:
[
{
"id": "test1",
"updated": "2015-11-30T16:51:54.988Z",
"status": "approved",
},
{
"id": "test2",
"updated": "2015-11-30T16:51:52.125Z",
"status": "approved",
},
{
"id": "test3",
"updated": "2015-11-30T16:51:50.469Z",
"status": "approved",
}
]
我需要能够指定一个id
来将我的结果偏移.
例如,如果status
为"已批准",count
为2,偏移id为"test1",则结果应为:
[
{
"id": "test2",
"updated": "2015-11-30T16:51:52.125Z",
"status": "approved",
},
{
"id": "test3",
"updated": "2015-11-30T16:51:50.469Z",
"status": "approved",
}
]
因此,我根据更新的属性进行排序,但结果应该只从偏移id之后的文档开始。
我想排除id。您不需要跳过它们,没有任何其他解决方案:
var query = Post.find({
$and: [{
status: status
}, {
id: {
$nin: ['test1']
}
}]
})
.sort('-updated')
.limit(count);
使用$nin
,您可以使用如下id数组排除多个id
:['test1', 'test2', 'etc...']
您不能通过特定的id进行偏移,但可以使用skip()
跳过一定数量的文档。
var query = Post.find()
.where('status').equals(status)
.sort('-updated')
.limit(count)
.skip(1); // <<< put the number of docs you want to skip here
如果你想跳过之前的所有文档并包括特定的id,你必须手动完成(如果你需要,我可以发布代码)。
编辑
跳过所有文档直到到达特定文档:
var query = Post.find()
.where('status').equals(status)
.sort('-updated')
.limit(count);
var offsetId = 'test1';
var filteredDocs = [];
query.exec().then(function (docs) {
var skipped = true;
docs.forEach(function (doc) {
if (skipped && doc.id == offsetId) skipped = false;
if (!skipped) filteredDocs.push(doc);
});
});