【问题标题】:Mongoose options for .find [duplicate].find 的猫鼬选项 [重复]
【发布时间】:2015-04-01 01:42:57
【问题描述】:

试图从 Mongoose 返回最后 10 个对象。无法弄清楚查找方法。有什么建议? {limit:10} 不起作用。

router.get('/tweets', function(req, res, next){
  Tweet.find({}, ['createdAt'], {limit:10}, function(err,objects) { res.json(objects)});
  
});

【问题讨论】:

  • 您是否打算根据createdAt 日期值获取最新的10 记录?如果是这种情况,则需要在方法链中按降序添加sort by createdAt 字段。

标签: mongoose find


【解决方案1】:

尝试在 find 上使用方法limit。

router.get('/tweets', function(req, res, next){
        Tweet.find({})
            .sort({'createdAt': -1})
            .limit(10)
            .exec(function(err,objects) { 
                res.json(objects)
            });
    });

【讨论】:

    猜你喜欢
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2013-04-20
    • 2019-07-10
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多