如何限制退回的商品数量?

Run*_*tle 101 mongoose

myModel.find({}, function(err, items){
console.log(items.length);    // big number
});
Run Code Online (Sandbox Code Playgroud)

如何将返回的项目限制为仅插入的最新10项?

mar*_*rni 171

在最新的mongoose中(写作时为3.8.1),你做两件事的方式不同:(1)你必须将单个参数传递给sort(),它必须是一个约束数组或只有一个约束,并且(2) )execFind()消失了,取而代之的是exec().因此,使用mongoose 3.8.1你会这样做:

var q = models.Post.find({published: true}).sort({'date': -1}).limit(20);
q.exec(function(err, posts) {
     // `posts` will be of length 20
});
Run Code Online (Sandbox Code Playgroud)

或者你可以像这样将它链接在一起:

models.Post
.find({published: true})
.sort({'date': -1})
.limit(20)
.exec(function(err, posts) {
     // `posts` will be of length 20
});
Run Code Online (Sandbox Code Playgroud)

  • @ArslArsl - 结果将按日期降序排序. (3认同)

kcb*_*ner 19

像这样,使用.limit():

var q = models.Post.find({published: true}).sort('date', -1).limit(20);
q.execFind(function(err, posts) {
  // `posts` will be of length 20
});
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,不知道你可以这样查询.我在哪里可以找到关于这个execFind方法的某种形式的文档? (2认同)
  • @Manny不是.请参阅marni的答案以获取更新版本. (2认同)

the*_*ode 9

我有点懒,所以我喜欢简单的事情:

let users = await Users.find({}, null,{limit: 50});
Run Code Online (Sandbox Code Playgroud)


小智 6

models.Post.find({published: true}, {sort: {'date': -1}, limit: 20}, function(err, posts) {
 // `posts` with sorted length of 20
});
Run Code Online (Sandbox Code Playgroud)

  • 尽管此代码段可能会解决问题,但包括*如何*和*为什么*的解释,这可以解决问题[将对您有很大帮助](// meta.stackexchange.com/q/114762),以提高您的帖子质量。请记住,您将来会为读者回答问题,而不仅仅是现在问的人!请[编辑]您的答案以添加解释,并指出适用的限制和假设。 (5认同)

rot*_*est 6

查找参数

\n\n

find函数的参数如下:

\n\n
    \n
  1. 状况\xc2\xabObject\xc2\xbb
  2. \n
  3. [projection]\xc2\xabObject|String\xc2\xbb要返回的可选字段,请参阅Query.prototype.select()
  4. \n
  5. [选项]\xc2\xabObject\xc2\xbb可选参见Query.prototype.setOptions()
  6. \n
  7. [打回来]\xc2\xabFunction\xc2\xbb
  8. \n
\n\n

如何限制

\n\n
const Post = require(\'./models/Post\');\n\nPost.find(\n  { published: true }, \n  null, \n  { sort: { \'date\': \'asc\' }, limit: 20 },\n  function(error, posts) {\n   if (error) return `${error} while finding from post collection`;\n\n   return posts; // posts with sorted length of 20\n  }\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

额外信息

\n\n

Mongoose 允许您以不同的方式查询您的集合,例如:\n官方文档

\n\n
// named john and at least 18\nMyModel.find({ name: \'john\', age: { $gte: 18 }});\n\n// executes, passing results to callback\nMyModel.find({ name: \'john\', age: { $gte: 18 }}, function (err, docs) {});\n\n// executes, name LIKE john and only selecting the "name" and "friends" fields\nMyModel.find({ name: /john/i }, \'name friends\', function (err, docs) { })\n\n// passing options\nMyModel.find({ name: /john/i }, null, { skip: 10 })\n\n// passing options and executes\nMyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});\n\n// executing a query explicitly\nvar query = MyModel.find({ name: /john/i }, null, { skip: 10 })\nquery.exec(function (err, docs) {});\n\n// using the promise returned from executing a query\nvar query = MyModel.find({ name: /john/i }, null, { skip: 10 });\nvar promise = query.exec();\npromise.addBack(function (err, docs) {});\n
Run Code Online (Sandbox Code Playgroud)\n