Mongodb / JS:找到最小(最早)的日期

时间:2016-05-16 10:07:58

标签: javascript mongodb datetime meteor

我试图找到集合中所有文档的最短日期。到目前为止,我已经用一个sort函数接近了这个,并且找到了我找到的第一个函数,请看这里:

earliestTime = function (kitUser) {
    'use strict';
    var res, earliestTime;
    res = Cards.findOne({ kit: kitUser }, { fields: { created: 1 } }, { sort: { created: 1 } });
    console.log('Created date: ' + res.created);
    earliestTime = new Date(res.created - 20000);
    console.log('Earliest time: ' + earliestTime);
    return earliestTime;
};

然后我从我找到的值中减去20秒,并将该值返回insert一个新文档,该文档在创建的字段中比我之前找到的最早的文件少20秒。

modifierObject = {};
modifierObject.created = earliestTime(kitUser);

当我查看控制台输出时,函数earliestTime会提供正确的结果:

I20160516-15:53:14.849(7)? Created date: Wed Apr 06 2016 12:00:11 GMT+0700 (ICT)
I20160516-15:53:14.849(7)? Earliest time: Wed Apr 06 2016 11:59:51 GMT+0700 (ICT)

但是,当在每次调用之间调用此函数21次并进行一些读/写操作时,每个插入的文档在创建的字段中具有相同的时间戳:

{ "_id" : "xcd3EfKfS6iLGvcsP", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "bvL3f8NHHZM8Ytdma", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "gAirEbicdWJz9CELB", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "kgAg6Jt2P89mTJYgN", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "xeqR5K2fxNmgEv4bb", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "PgPimNRxj2zBuS8M2", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "snwEWeXpn4Ampsito", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "8RCS8YQLxY7tY6Ruk", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "tEqRJs49RNvCzkMz6", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "PJnZ7Z4WH6rTTw626", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "J2L6KPhzxcva3rttJ", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "YR5R6ShakPxCXgj54", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "Ajd6D8E8WrRB47q7d", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "g5CQgjbPH7NoytyMw", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "s9NgtsiR7WbjgeKLr", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "NPtZMHkAdqySShD3a", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "XxpWREjutAjbgHyME", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "6ZsiL2ZgidWjk3cgb", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "pvXiEaJgkuHEKijbB", "created" : ISODate("2016-04-06T04:59:51.183Z") }
{ "_id" : "im4GzumrE6RARNLbe", "created" : ISODate("2016-04-06T04:59:51.183Z") }

我很想知道如何发生这种情况。我期待的结果是(时间戳):

"created" : ISODate("2016-04-06T04:59:51.183Z")
"created" : ISODate("2016-04-06T04:59:41.183Z")
"created" : ISODate("2016-04-06T04:59:31.183Z")
"created" : ISODate("2016-04-06T04:59:21.183Z")

我怀疑我的功能与排序有关,因此我的问题是如何在MongoDB中更容易找到日期的最小值。

提前致谢!

1 个答案:

答案 0 :(得分:1)

好的,我在投影中发现了一个错误,第四行必须是:

res = Cards.findOne({ kit: kitUser }, { fields: { created: 1 }, sort: { created: 1 } }); 

我忽略了投影是一个对象,因此第一个括号必须围绕fieldssort等所有选项。

这篇文章非常有助于识别它,并感谢Marcel Fahle指出它:

https://themeteorchef.com/snippets/mongodb-queries-and-projections/