title: How To Query Mongoose Database With Specific Time Range
body: <p>I have an order model and I want to fetch all orders that are created at last month to current date. Like receiving all orders from 1 may, 2021 to 1 jun 2021.</p>
  <p>My database modal has <code>{timestamps: true}</code> option which adds <code>createdAt</code> and <code>updatedAt</code> fields.</p>
  <p>Order.js</p>
  <pre><code>const OrderSchema = new mongoose.Schema(
    {
      billingAddress: { type: Object, required: true },
      deliveryAddress: { type: Object, required: true },
      user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: &quot;User&quot;,
      },
      seller: {
        type: mongoose.Schema.Types.ObjectId,
        ref: &quot;Shop&quot;,
      },
      Product: {
        product: { type: mongoose.Schema.Types.ObjectId, ref: &quot;Product&quot; },
        color: String,
        quantity: Number,
      },
      invoiceId: { type: String, required: true },
      paymentIntentId: { type: String, required: true },
      totalAmount: { type: Number, required: true },
      groupId: { type: String, required: true },
      status: {
        type: String,
        enum: [
          &quot;waitingConfirmation&quot;,
          &quot;confirmed&quot;,
          &quot;cancelRequest&quot;,
          &quot;cancelled&quot;,
          &quot;packing&quot;,
          &quot;shipped&quot;,
          &quot;delivered&quot;,
        ],
        default: &quot;waitingConfirmation&quot;,
      },
      note: String,
    },
    { timestamps: true }
  );
  </code></pre>
  <p>When I do this to fetch last month's data it works</p>
  <pre><code>const lastMonthOrders = await Order.find({
      $and: [
        { seller: req.shop.id },
        {
          createdAt: {
            $gte: new Date(2021, 4, 1),
            $lt: new Date(2021, 5, 1),
          },
        },
      ],
    });
  </code></pre>
  <p>But I want my dates to be dynamic so I tried this</p>
  <pre><code>  let today = new Date();
    let lastMonth = new Date(
      today.setMonth(today.getMonth() - 1) - 60 * 60 * 1000
    );
  
  const lastMonthOrders = await Order.find({
      $and: [
        { seller: req.shop.id },
        {
          createdAt: {
            $gte: lastMonth,
            $lt: today,
          },
        },
      ],
    });
  </code></pre>
  <p>timestamps in my model look like this, and their data type is Date:</p>
  <pre><code>createdAt: 2021-06-02T10:20:26.984+00:00,
  updatedAt: 2021-06-02T10:21:28.432+00:00
  </code></pre>
  <p>The code above doesnt work. It returns an empty object. How to fetch data from a specific time range?</p>