首页 文章

在mongoDB中使用Monk限制查找

提问于
浏览
2

我有大量的文件 . 我想获得前100个 . 从Monk Docs,这是我正在使用的查找方法

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

这非常浪费,因为无论如何都要检索整个文档 . 我想要这样的东西(来自mongodb docs)

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

我试过和尚,

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

但它给出了一个错误,表示在它之前返回的“promise”对象中没有函数限制 .

Monk是否有这样的选项限制文件数量?

2 回答

  • 2

    是的,您可以在第二个参数中将其作为选项传递:

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

    这来自本机节点mongodb驱动程序,monk通过mongoskin包装:

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

  • 3

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

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

相关问题