findOne在utc中查询比较日期

时间:2017-04-14 07:29:47

标签: node.js mongodb mongoose

我在我的应用程序中使用UTC日期格式并存储数据,如下所示

{ 
"_id" : ObjectId("58ef69c221f24305c0c7123b"), 
"departmentId" : "58db361424f6bc2d3840f38b", 
"departmentOfficerId" : "58e21f0a7fa219021cd351ca", 
"departmentOfficerCurrentMonth" : NumberInt(1), 
"calenderYear": 2017 
"createdAt" : "Thu, 13 Apr 2017 12:06:26 GMT", 
"updatedAt" : "Thu, 13 Apr 2017 12:06:26 GMT", 
}

它不应该给我任何数据,但它给了我整个数据。

我正在使用以下mongodb查询

db.departmentOfficerMonthlyScores.findOne(
{ 
 departmentOfficerId: "58e21f0a7fa219021cd351ca",
 departmentOfficerCurrentMonth: 1,
 calenderYear: 2017, 
 updatedAt: { "$gte" : "Fri, 14 Apr 2017 06:33:10 GMT" } 
}
)

但是当我使用此查询时,提供比较的日期是在同一天

db.departmentOfficerMonthlyScores.findOne(
{ 
 departmentOfficerId: "58e21f0a7fa219021cd351ca",
 departmentOfficerCurrentMonth: 1,
 calenderYear: 2017, 
 updatedAt: { "$gte" : "Thu, 13 Apr 2017 13:16:20 GMT" } 
}
)

它给了我正确的结果,即null。如何处理这个

4 个答案:

答案 0 :(得分:0)

最近开始学习mongo,node和mongoose,但看起来你的代码正在进行字符串比较。

我建议您在日期字段中使用日期数据(如果它与您的架构中的不一样),这会使日期比较工作。

当您在过滤器中与$ gte进行比较时,将该字符串日期解析为日期对象,如

  

新日期('1995年12月17日03:24:00')

同时进行搜索,我发现此帖https://stackoverflow.com/a/20911237/2928459显示了如何在搜索时转换存储的数据类型。我假设,如果你将你的字符串数据转换为Date类型并与JavaScript Date对象进行比较,它应该可以解决。

答案 1 :(得分:0)

您可以通过

获取当前日期来使用日期

1> new Date()

OR

2 - ; new Date

获取当前的Date对象,以便您可以轻松地与MongoDB数据库中存储的日期进行比较。

此外,如果您想在日期对象中进行更多修改,请使用moment Library.It对于Date来说非常出色。

由于

答案 2 :(得分:0)

使用日期查询时,您应该使用"{\"flag\":true,\"message\":\"Image\",\"blobResource\":{\"id\":\"53269290-1f79-11e7-ac4a-a860b602ab0a\",\"blob_type\":\"jpeg\",\"created_by\":\"f7c6f7e2-1090-11e7-93ae-92361f002671\",\"created_on\":\"Apr 12, 2017 5:43:17 PM\",\"enable\":true,\"name\":\"f7c6f7e2-1090-11e7-93ae-92361f002671.png\",\"resource\":[80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,2,109,0,0,2,109,8,6,0,0,0,100,11,71,-66,0,0,0,1,115,82,71,66,0,-82,-50,28,-23,0,0,0,28,105,68,79,84,0,0,0,2,0,0,0,0,0,0,1,55,0,0,0,40,0,0,1,55,0,0,1,54,0,7,-107,-123,25,59,-111,-21,0,0,64,0,73,68,65,84,120,1,100,-67,-25,127,85,-25,-71,-83,-115,-23,-94,-9,-34,59,66,116,16,106,72,72,-128,68,7,81,5,-110,16,69,84,1,-94,-119,34,-44,-69,-124,122,47,-120,-34,-117,11,24,27,108,-29,94,19,39,118,122,79,-20,36,123,103,103,-97,125,-50,-5,39,-36,-17,53,-98,41,-31,-28,-100,15,-49,111,-82,-66,-26,90,107,-82,103,94,115,-116,113,-33,-77,-61,-25,-65,-8,-93,125,-11,-21,-65,-40,-41,-116,-97,-2,-26,59,-5,-39,-17,-65,-77,95,-3,-7,-17,-10,-37,-65,-4,-89,-3,-15,-81,-1,101,127,-7,-113,-1,101,127,-3,-57,-1,-74,-65,-2,23,-125,-27,-9,-1,-7,63,-10,29,-73,-3,-27,-17,-1,116,-9,121,-105,-1,-101,-21,-34,-8,-29,-9,-1,-80,63,48,-76,-4,-19,-97,-1,102,-65,-2,-35,95,-19,23,-65,-7,-34,-66,-2,-7,31,-19,-77,-97,-2,-58,62,-4,-14,23,-10,-30,-77,111,-20,-35,79,-66,-74,119,62,-2,-38,-98,127,-12,99,-106,26,95,-37,-77,15,-65]}}

中的日期

示例:

new Date()

这样它就能正常工作。

答案 3 :(得分:0)

如果要将值作为日期操作,则应存储日期,但不应存储表示日期的字符串。同样在查询中,您应该使用日期对象,但不能使用表示日期的字符串。

看一下例子(mongodb shell):

db.test_date.insert({date: Date()})
WriteResult({ "nInserted" : 1 })

db.test_date.insert({date: Date()})
WriteResult({ "nInserted" : 1 })

db.test_date.insert({date: Date()})
WriteResult({ "nInserted" : 1 })

db.test_date.find()
{ "_id" : ObjectId("58f0bc95ad51c90cceb8e092"), "date" : "Fri Apr 14 2017 15:12:05 GMT+0300 (MSK)" }
{ "_id" : ObjectId("58f0bc97ad51c90cceb8e093"), "date" : "Fri Apr 14 2017 15:12:07 GMT+0300 (MSK)" }
{ "_id" : ObjectId("58f0bc97ad51c90cceb8e094"), "date" : "Fri Apr 14 2017 15:12:07 GMT+0300 (MSK)" }

typeof db.test_date.findOne().date
string

db.test_date.find({date: {$gt: "A"}})
{ "_id" : ObjectId("58f0bc95ad51c90cceb8e092"), "date" : "Fri Apr 14 2017 15:12:05 GMT+0300 (MSK)" }
{ "_id" : ObjectId("58f0bc97ad51c90cceb8e093"), "date" : "Fri Apr 14 2017 15:12:07 GMT+0300 (MSK)" }
{ "_id" : ObjectId("58f0bc97ad51c90cceb8e094"), "date" : "Fri Apr 14 2017 15:12:07 GMT+0300 (MSK)" }

db.test_date.find({date: {$lt: "A"}})
"there are no documents to this condition"

db.test_date.remove({})
WriteResult({ "nRemoved" : 3 })

db.test_date.insert({date: new Date()})
WriteResult({ "nInserted" : 1 })

db.test_date.insert({date: new Date()})
WriteResult({ "nInserted" : 1 })

db.test_date.find()
{ "_id" : ObjectId("58f0bd9cad51c90cceb8e095"), "date" : ISODate("2017-04-14T12:16:28.296Z") }
{ "_id" : ObjectId("58f0bd9dad51c90cceb8e096"), "date" : ISODate("2017-04-14T12:16:29.456Z") }

typeof db.test_date.findOne().date
object

db.test_date.find({date: {$gt: new Date('2017-01-01')}})
{ "_id" : ObjectId("58f0bd9cad51c90cceb8e095"), "date" : ISODate("2017-04-14T12:16:28.296Z") }
{ "_id" : ObjectId("58f0bd9dad51c90cceb8e096"), "date" : ISODate("2017-04-14T12:16:29.456Z") }

Date()
Fri Apr 14 2017 15:18:39 GMT+0300 (MSK)
typeof Date()
string
typeof new Date()
object