如何获取mongoose.js中的最新和最旧记录(或者只是它们之间的时间跨度) [英] How to get the latest and oldest record in mongoose.js (or just the timespan between them)

查看:249
本文介绍了如何获取mongoose.js中的最新和最旧记录(或者只是它们之间的时间跨度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆记录,我需要得到最新的(最近的)和最旧的(最近的)。

I have a bunch of records and I need to get latest (most recent) and the oldest (least recent).

谷歌搜索时发现这个主题我看到了几个问题:

When googling I found this topic where I saw a couple of queries:

// option 1
Tweet.findOne({}, [], { $orderby : { 'created_at' : -1 } }, function(err, post) {
  console.log( post );
});
// option 2
Tweet.find({}, [], {sort:[['arrival',-1]]}, function(err, post) {
  console.log( post );
});

不幸的是他们都错了:

TypeError: Invalid select() argument. Must be a string or object.

链接也有这个:

Tweet.find().sort('_id','descending').limit(15).find(function(err, post) {
  console.log( post );
});

且一个错误:

TypeError: Invalid sort() argument. Must be a string or object.

那我怎样才能获得这些记录?

So how can I get those records?

更理想的是,我只想要最旧和最新记录之间的时间差(秒?),但我不知道如何开始制作像这样的查询。

Even more ideally I just want the difference in time (seconds?) between the oldest and the newest record, but I have no clue on how to start making a query like that.

这是架构:

var Tweet = new Schema({
    body: String
  , fid: { type: String, index: { unique: true } }
  , username: { type: String, index: true }
  , userid: Number
  , created_at: Date
  , source: String
});

我很确定我拥有最新版本的mongoDB和mongoose。

I'm pretty sure I have the most recent version of mongoDB and mongoose.

这是根据JohnnyHK提供的答案计算时间跨度的方式:

This is how I calc the timespan based on the answer provided by JohnnyHK:

var calcDays = function( cb ) {
  var getOldest = function( cb ) {
    Tweet.findOne({}, {}, { sort: { 'created_at' : 1 } }, function(err, post) {
      cb( null, post.created_at.getTime() );
    });
  }
    , getNewest = function( cb ) {
    Tweet.findOne({}, {}, { sort: { 'created_at' : -1 } }, function(err, post) {
      cb( null, post.created_at.getTime() );
    });
  }

  async.parallel({ 
    oldest: getOldest
  , newest: getNewest
  }
    , function( err, results ) {
      var days = ( results.newest - results.oldest ) / 1000 / 60 / 60 / 24;
      // days = Math.round( days );
      cb( null, days );
    }
  );
}


推荐答案

Mongoose 3.x正在抱怨关于 [> code>调用中的 [] 参数,因为选择字段的参数不再支持数组格式要包含。

Mongoose 3.x is complaining about the [] parameter in your findOne calls as the array format is no longer supported for the parameter that selects the fields to include.

请尝试使用此代码来查找最新内容:

Try this instead to find the newest:

Tweet.findOne({}, {}, { sort: { 'created_at' : -1 } }, function(err, post) {
  console.log( post );
});

-1 更改为 1 找到最旧的。

但是因为你没有使用任何字段选择,所以链接一对夫妇更加清晰一起打电话:

But because you're not using any field selection, it's somewhat cleaner to chain a couple calls together:

Tweet.findOne().sort({created_at: -1}).exec(function(err, post) { ... });

甚至将字符串传递给 sort

Or even pass a string to sort:

Tweet.findOne().sort('-created_at').exec(function(err, post) { ... });

这篇关于如何获取mongoose.js中的最新和最旧记录(或者只是它们之间的时间跨度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆