# Calculations with MongoDB dates

**URL:** https://forum.kirupa.com/t/calculations-with-mongodb-dates/644580
**Category:** programming
**Created:** [October 16, 2020, 7:39pm UTC](https://forum.kirupa.com/t/calculations-with-mongodb-dates/644580 "2020-10-16T19:39:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)
#### Post date: [October 16, 2020, 7:39pm UTC](https://forum.kirupa.com/t/calculations-with-mongodb-dates/644580/1 "2020-10-16T19:39:09Z")

</div>

I need to create a View in MongoDB which only shows documents created within the last year. Using fixed dates, that is easy. I can simply:

```auto
db.comments.find({
    dtCreated: {
        $gte: '2019-10-16 00:00:00'
    }
})

```

But how do I make it so it is always one year ago from the current date? For example:

```auto
db.comments.find({
    dtCreated: {
        $gte: ISODate() - 365
    }
})

```

Thanks in advance for any suggestions.

---

<div class="post-metadata">

### Author: ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)
#### Post date: [October 20, 2020, 1:10am UTC](https://forum.kirupa.com/t/calculations-with-mongodb-dates/644580/2 "2020-10-20T01:10:52Z")

</div>

I’ve managed to get the right formula. Here’s what I did:

`$gte: new Date((new Date()).getTime() - (366 * 24 * 60 * 60 * 1000))`

Unfortunately, that does not get inserted in the view structure as a formula that is calculated every time the view is accessed. Instead, it gets calculated upon creation and the actual result of the formula is inserted in the view structure. In other words, the view is always returning data from that date, which was calculated when the view was created.

Any suggestions?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [October 20, 2020, 3:53am UTC](https://forum.kirupa.com/t/calculations-with-mongodb-dates/644580/3 "2020-10-20T03:53:56Z")

</div>

Unfortunately, I haven’t used mongoDB in years to be able to help. These forums are typically are more front-end focused, so a database-related question doesn’t have the right set of eyes on it ☹

---

<div class="post-metadata">

### Author: ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)
#### Post date: [October 26, 2020, 3:55pm UTC](https://forum.kirupa.com/t/calculations-with-mongodb-dates/644580/4 "2020-10-26T15:55:35Z")

</div>

It is OK. It is a great community and I am glad to be part of it. Someone kindly pointed me to the right direction, involving the use of the $$NOW system variable. For completion, in case someone is interested, here is the code that solved the problem:

```auto
[ { 
  $match: { 
      $expr: { 
          $gte: ["$created", { $subtract: [ "$NOW", 31536000000] } ] 
      } 
  } 
} ]

```

The number ‘31536000000’ being simply the result of 365 days in milliseconds (365 \* 24 \* 60 \* 60 \* 1000). It worked like a charm. Many thanks and all credit to [Prasad\_](https://stackoverflow.com/users/4679320/prasad).
