Categories
JavaScript Answers

How to query ISODate fields with Node.js MongoDB?

Spread the love

Sometimes, we want to query ISODate fields with Node.js MongoDB.

In this article, we’ll look at how to query ISODate fields with Node.js MongoDB.

How to query ISODate fields with Node.js MongoDB?

To query ISODate fields with Node.js MongoDB, we can use JavaScript date objects.

For instance, we write

const cursor = collection.find({
  title,
  dateCreated: {
    "$gte": new Date("2020-10-01T00:00:00.000Z"),
    "$lt": new Date("2022-03-13T16:17:36.470Z")
  }
});

to call find to query dateCreated greater than or equal to "2020-10-01T00:00:00.000Z" and less than "2022-03-13T16:17:36.470Z".

We create the date objects from the date strings by passing them into the Date constructor.

Conclusion

To query ISODate fields with Node.js MongoDB, we can use JavaScript date objects.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *