gpt4 book ai didi

javascript - MongoDB/JS : find the minimum (earliest) date

转载 作者:行者123 更新时间:2023-12-03 06:56:35 25 4
gpt4 key购买 nike

我正在尝试查找集合中所有文档的最短日期。到目前为止,我已经通过排序函数解决了这个问题,并采用了我找到的第一个函数,请参见此处:

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 秒,并将该值返回到插入一个新文档,该文档在创建的字段中比我之前找到的最早的字段少了 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 中的最小日期。

提前致谢!

最佳答案

好的,我发现我的投影有错误,第四行必须是:

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

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

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

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

关于javascript - MongoDB/JS : find the minimum (earliest) date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com