在我的MongoDB集合我有一個紀錄:如何在mongodb的日期範圍內獲得記錄?
//mongodb field: "birth_date": "1983-05-06T16:26:32.613Z"
在這裏,我找到的查詢,以獲得在該範圍的記錄:
var birthYear = 1983;
var birthDateStart = new Date('1.1.' + birthYear); //Sat Jan 01 1983 00:00:00 GMT+0100 (Mitteleuropäische Zeit)
var birthDateEnd = new Date('12.30.' + birthYear); //Fri Dec 30 1983 00:00:00 GMT+0100 (Mitteleuropäische Zeit)
var cursor = db.collection('users').find({
birth_date: {$gte: birthDateStart, $lt: birthDateEnd}
})
我認爲這個問題是日期格式,如何我可以得到與數據庫中相同的Date()格式嗎?
我以前variety獲得DB模式:
+--------------------------------------------------+
| key | types | occurrences | percents |
| ------------ | -------- | ----------- | -------- |
| _id | ObjectId | 1 | 100.0 |
| bio | String | 1 | 100.0 |
| birth_date | String | 1 | 100.0 |
+--------------------------------------------------+
我用的是 'MongoDB的' 包express.js - 而不能使用ISODate()。
ReferenceError: ISODate is not defined
正是在這種格式。您正在查看「串化」表單。但是你的數據本身可能是字符串。從[Mongo Shell]中查看您的實際數據(https://docs.mongodb.com/manual/reference/program/mongo/#bin.mongo)。這將確定你的''birth_date'數據實際上是什麼樣子 –
也應該是'new Date(「yyyy-mm-dd」)'在UTC時間段中實際構造一個Date對象作爲你的MongoDB數據應該被存儲,但是我們需要看到數據的實際表示。 –
[在兩個日期之間查找對象MongoDB]可能的重複(https://stackoverflow.com/questions/2943222/find-objects -between-dates-mongodb) –