我收集了大量的文件。我想要这些的头100个。从Monk Docs,这是我正在使用的查找方法
var documents = [];
users.find({}, function (err, docs){
for(i=0;i<100;i++)
documents.push(docs[i]);
});这是非常浪费的,因为无论如何都会检索到整个文档。我想要这样的东西(从mongodb文档)
docs = db.users.find().limit( 100 );我试过和尚,
users.find({}, function (err, docs){
for(i=0;i<docs.length;i++)
documents.push(docs[i]);
}).limit(100);但是,它给出了一个错误,即在它之前返回的“诺言”对象中没有函数限制。
在Monk中是否有限制文档数量的选择?
发布于 2014-08-11 20:38:23
是的,您可以在第二个参数中将其作为选项传递:
users.find({}, { limit : 100 }, function (err, docs){
for(i=0;i<docs.length;i++)
documents.push(docs[i]);
});这来自本机节点mongodb驱动程序,mongodb驱动程序由mongoskin封装:
http://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#query-options
发布于 2014-08-11 20:38:14
您可以将options对象作为第二个参数传递给.find()
users.find({}, {limit: 100}, next);发布于 2022-09-19 17:42:03
如果您想要添加分页编号和排序,下面是如何使用僧:
users.find({}, {limit: 100, skip: pagenumber, sort: {'username': 1}}) 其中限制页的大小,跳过是页码,排序结果按用户名升序。
https://stackoverflow.com/questions/25251871
复制相似问题