gpt4 book ai didi

node.js - Mongoose/node.js 我如何查询范围 [20 到 30 之间的元素]

转载 作者:可可西里 更新时间:2023-11-01 10:03:52 26 4
gpt4 key购买 nike

我的 mongodb 表中有 500 个元素,例如,我只想选择前 100 个元素之后的 100 个元素。为此,请尝试使用 .slice 等。但此查询无法正常工作。我正在使用 Mongoose 和 node.js:

var query2 = JenkinsTestModel.find();
query2.where('jobname date').slice([-100 /*skip*/, 100 /*limit*/]) //i tried several different ways here
query2.exec(function (err, job) {
if (err) throw err;
console.log(job);
});

此查询返回所有元素。我该如何解决上述问题?

最佳答案

您可以使用 skip() limit() 方法:

var query2 = JenkinsTestModel.find()
.where('jobname date')
.skip(100)
.limit(100)
.exec(function (err, job) {
if (err) throw err;
console.log(job);
});

注意:来自 docs

The cursor.skip() method is often expensive because it requires the server to walk from the beginning of the collection or index to get the offset or skip position before beginning to return result. As offset (e.g. pageNumber above) increases, cursor.skip() will become slower and more CPU intensive. With larger collections, cursor.skip() may become IO bound.

关于node.js - Mongoose/node.js 我如何查询范围 [20 到 30 之间的元素],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29126411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com