Mongoose is not showing the ISODate in the result
-
04-03-2021 - |
Question
I am using mongodb version 4.2.2
.
I have following data:
{
"_id" : ObjectId("5f0c58089c058304f7a64fa9"),
"date" : ISODate("2021-07-11T00:00:00.000Z"),
"dayOfWeek" : 0,
"staffMember" : ObjectId("5f0c58076b559419d62efe47"),
"startTime" : 885
},
/* 2 createdAt:2020-07-13T12:48:08.000Z*/
{
"_id" : ObjectId("5f0c58089c058304f7a64fa8"),
"date" : ISODate("2021-07-11T00:00:00.000Z"),
"dayOfWeek" : 0,
"staffMember" : ObjectId("5f0c58076b559419d62efe47"),
"startTime" : 870
},
/* 3 createdAt:2020-07-13T12:48:08.000Z*/
{
"_id" : ObjectId("5f0c58089c058304f7a64fa7"),
"date" : ISODate("2021-07-11T00:00:00.000Z"),
"dayOfWeek" : 0,
"staffMember" : ObjectId("5f0c58076b559419d62efe47"),
"startTime" : 855
},
/* 4 createdAt:2020-07-13T12:48:08.000Z*/
{
"_id" : ObjectId("5f0c58089c058304f7a64fa6"),
"date" : ISODate("2021-07-11T00:00:00.000Z"),
"dayOfWeek" : 0,
"staffMember" : ObjectId("5f0c58076b559419d62efe47"),
"startTime" : 840
}
I am using mongoose
in my project. When I run this query in node js:
const { memberId, date } = req.params;
let isoDate = date.toISOString();
let timeSlots = await TimeSlots.find(
{
staffMember: memberId,
date: isoDate
});
The result printed on the console
:
db.spaTimeSlots.find({"staffMember" : ObjectId("5f0c58076b559419d62efe47"), date: new Date("Wed, 11 Jul 2020 00:00:00 GMT")})
.projection({})
.sort({_id:-1})
.limit(100)
However, I want this result:
db.spaTimeSlots.find({"staffMember" : ObjectId("5f0c58076b559419d62efe47"), date: ISODate("2021-07-11T00:00:00.000Z")})
.projection({})
.sort({_id:-1})
.limit(100)
I want the result returned with the date in ISODate
format, not as text (i.e. not Wed, 11 Jul
).
Solution
You need to add like this:
let date = new Date().toISOString();
let isoDate = new Date(date);
Hope it works.
OTHER TIPS
ISODate
is a mongo
shell type, it does not exist in Node.
You can format the JS Date
object any way you wish in your application.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange