1

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).

1
  • This question should have been asked on SO.
    – D. SM
    Jul 15 '20 at 2:59
4

You need to add like this:

let date = new Date().toISOString();
let isoDate = new Date(date);

Hope it works.

1
  • 2
    Thanks, It works for me. Sep 2 '20 at 7:36
1

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.

1
  • Actually ISODate() is synonym for Javascript new Date(), i.e.it is a Date object. Jul 16 '20 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.