2017-04-14 30 views
0

比較日期我使用UTC日期格式在我的應用程序和存儲數據如下圖所示findOne查詢在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" } 
} 
) 

它給我正確的結果即空。如何處理這個問題

回答

0

最近開始學習mongo,node和mongoose,但它看起來像你的代碼正在做字符串比較。

我會建議使用日期數據作爲日期字段(如果它不像那樣在你的模式中),它應該進行日期比較工作。

此外,當你在你的過濾器$ GTE比較,分析該字符串日期像

新日期的日期對象( '1995年12月17日3點24分○○秒')

也做了搜索,我發現這個職位https://stackoverflow.com/a/20911237/2928459它顯示瞭如何轉換你的存儲數據類型,同時搜索。我假設,如果您將字符串數據轉換爲Date類型,並與JavaScript Date對象進行比較,則應該解決問題。

0

您可以通過

1>new Date()

OR

通過獲取當前的日期使用日期

2>new Date

獲取當前日期對象,以便您可以用日期很容易地比較存儲在MongoDB數據庫中。

此外,如果您想在日期對象中進行更多修改,然後使用moment Library.It爲Date創造出色的工作。

感謝

0

當你與日期查詢,你應該使用日期內new Date()

例子:

var queryDate = new Date('2017-04-14'); //This can be any valid format so create you date you want to query and pass it inside. 

db.departmentOfficerMonthlyScores.findOne({ 
    departmentOfficerId: "58e21f0a7fa219021cd351ca", 
    departmentOfficerCurrentMonth: 1, 
    calenderYear: 2017, 
    updatedAt: { "$gte" : queryDate } 
}) 

這樣,它會正常工作。

0

如果您想將值作爲日期進行操作,則應該存儲日期,而不是表示日期的字符串。同樣在查詢中,您應該使用日期對象,但不能使用表示日期的字符串。

查看例(mongodb的殼):

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