Mongoose/node.js 我如何查询范围 [ 20 到 30 之间的元素 ]
Mongoose/node.js How can i query in range [ elements between 20 and 30 ]
我的 mongodb table 中有 500 个元素,我想 select 例如前 100 个元素之后只有 100 个元素。为此,请尝试使用 .slice
等。但此查询无法正常工作。我正在使用猫鼬和 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()
and the 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.
我的 mongodb table 中有 500 个元素,我想 select 例如前 100 个元素之后只有 100 个元素。为此,请尝试使用 .slice
等。但此查询无法正常工作。我正在使用猫鼬和 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()
and the 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.