Hadi_N_A
(Hadi N/A)
1
Hi
I store date like this:
await db.collection("posts").insertOne({date: new Date()})
storing like this didn’t work and I had error:
await db.collection("posts").insertOne({date: ISODate()})
await db.collection("posts").insertOne({date: new ISODate()})
And then read it like this:
const data = await db
.collection('posts')
.find()
.sort({_id: -1})
.toArray()
And I get the result as a string (not date object):
date: ‘2023-11-26T08:12:32.469Z’
But I want to return date as the date object, not as a string!
Mongodb version: 6.2.0
Thank you in advance
chris
(Chris Dellaway)
2
That would be fairly unusual. Using data inserted the same way you have I get expected Date objects returned:
> (await db
.collection("posts")
.find()
.sort({ _id: -1 })
.toArray()
).forEach(
x => console.log(
`date: ${x.date}, isDate: ${x.date instanceof Date}`
)
)
date: Sun Nov 26 2023 16:08:58 GMT+0000 (Coordinated Universal Time), isDate: true
date: Sun Nov 26 2023 15:05:41 GMT+0000 (Coordinated Universal Time), isDate: true
Hadi_N_A
(Hadi N/A)
3
Exactly!
It’s so weird. This’s a very simple thing but it returns string, not a date object