简体   繁体   English

在 mongoDB 中使用 Monk 限制查找

[英]Limit find using Monk in mongoDB

I have a large collection of documents.我有大量的文件。 I want to get the first 100 of these.我想获得其中的前 100 个。 From the Monk Docs, this is the find method I am using来自 Monk Docs,这是我正在使用的查找方法

var documents = [];
users.find({}, function (err, docs){
  for(i=0;i<100;i++)
     documents.push(docs[i]);
});

This is highly wasteful since, the entire documents are anyway retrieved.这是非常浪费的,因为无论如何都要检索整个文档。 I want something like this (from the mongodb docs)我想要这样的东西(来自 mongodb 文档)

docs =  db.users.find().limit( 100 );

I tried in monk,我试过和尚,

users.find({}, function (err, docs){
  for(i=0;i<docs.length;i++)
     documents.push(docs[i]);
}).limit(100);

But it gives an error saying that there is no function limit in the "promise" object that is returned before it.但它给出了一个错误,说在它之前返回的“承诺” object 中没有 function 限制。

Is there such an option in Monk to limit the number of documents? Monk中是否有这样的选项来限制文档的数量?

Yes, you can pass it as an option in the second parameter: 是的,您可以在第二个参数中将其作为选项传递:

users.find({}, { limit : 100 }, function (err, docs){
  for(i=0;i<docs.length;i++)
     documents.push(docs[i]);
});

This comes from the native node mongodb driver, which monk wraps via mongoskin: 这来自于本机节点mongodb驱动程序,该驱动程序和尚通过mongoskin包装:

http://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#query-options http://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#query-options

您可以将options对象作为第二个参数传递给.find()

users.find({}, {limit: 100}, next);

If you want add pagenumber and sorting, here is how to do it with monk:如果你想添加页码和排序,这里是如何用和尚做的:

users.find({}, {limit: 100, skip: pagenumber, sort: {'username': 1}})  

where limit is for page size, skip is page number, sorting result by username ascending.其中limit是页面大小,skip是页码,按用户名升序排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM