mongoose - 如何在猫鼬跳过和限制之前排序?
问题描述
const list = async function({
end_cursor = 0
} = {}) {
return await db.collection.find().sort({ _id: -1 }).skip(end_cursor).limit(50);
}
上面是我认为它应该工作的代码,但我发现它没有。这是显而易见的:
let list1 = await list(0);
list1 = list1.reduce((o, a)=>{
let {
_id
} = a;
o[_id] = true;
return o;
}, {});
let list2 = await list(50);
for (let i of list2) {
let {
_id
} = i;
if (list1[_id]) {
console.log('repeated');
break;
}
}
如果db.collection.find().sort({ _id: -1 }).skip(end_cursor).limit(50)
有效, list1 和 list2 应该有唯一_id
的 s,但他们没有,而是控制台输出repeat
。
解决方案
您可以根据您的要求使用聚合。
db.collection.aggregate([
{$sort: {_id: -1}},
{$skip: end_cursor}, //value should be a positive integer
{$limit: 50}
]);
推荐阅读
- python - pool.apply_async 和全局变量
- puppeteer - Puppeteer:如何在使用 page.click() 创建的新选项卡加载之前更改导航器属性
- python - ValueError: Axes instance argument was not found in a figure, Question with same name has no answer
- javascript - 使用 puppeteer 加载 chrome 扩展
- oracle-apex - 多行上的 Oracle APEX RDS 选项卡而不是滚动
- c - 无法使用 .asm 文件构建 CLion 项目
- python - 如何使pyinstaller不使用anaconda并构建一个小型exe文件
- python - TypeError: '_io.TextIOWrapper' 类型的对象没有 len ()
- angular - 为 ng-bootstrap modal (NgbModal) [Angular 6] 编写单元测试
- html - 使用 ngfor 数组数据在 Angular 中动态更改样式