我目前有以下查询来返回给定状态
的计数
个最近更新的帖子。
var query = Post.find()
.where('status').equals(status)
.sort('-updated')
.limit(count);
如果 status
为“approved”且 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
为“approved”,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
,您可以使用如下所示的 ids 数组来排除多个 id
:['test1', 'test2', 'etc.. .']
关于javascript - 如何偏移 Mongoose 排序查询以在特定文档之后开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018137/