# Post endpoint workflow mongodb find

**URL:** <https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936>\
**Category:** Help\
**Tags:** workflow, mongodb\
**Created:** [July 17, 2021, 7:31pm UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936 "2021-07-17T19:31:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 17, 2021, 7:31pm UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/1 "2021-07-17T19:31:31Z")

</div>

Hi,

I have a post workflow that is searching a mongodb for matching devices. I’m struggling with including a $gt and $lt for a date range in the query.

workflow:  
endpoint  
mongodb: find with this as the json template:

{  
“serial”: “{{data.body.serial}}”,  
“chamber”: “{{data.body.chamber}}”,  
“time”: “{$gt: ISODate(’{{data.body.startDate}}’)}”  
}

if I take out the “time” field it returns exactly what I expect–all matches for serial and chamber. But every format I try with the “time” field just returns nothing. I’ve tried everything I can think of with the $gt section but can’t land on one that works. any ideas?

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [July 19, 2021, 2:39pm UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/3 "2021-07-19T14:39:27Z")

</div>

Hey @Tim_Brennan,

This is a difficult question as I don’t know much about your MongoDB setup. If you use the query you sent with a hardcoded date, rather than `ISODate('{{data.body.startDate}}')`, do you get any results?

Also, it looks like you’ve got a little bit of incorrect JSON. With your query, you are trying to compare time to the string `"{$gt: ISODate(’{{data.body.startDate}}’)}”` which will not work. Your query should look like this:

```auto
{
 “serial”: “{{data.body.serial}}”,
 “chamber”: “{{data.body.chamber}}”,
 “time”: {
   "$gt": ISODate(’{{data.body.startDate}}’)
 }
}

```

If `time` is a standard Mongo time field, then I would suggest you use:

```auto
{
  “serial”: “{{data.body.serial}}”,
  “chamber”: “{{data.body.chamber}}”,
  “time”: {
    "$gt": {{formatDate data.body.startDate 'x'}}
  }
}

```

The above does assume that `data.body.startDate` is a date or a date-like string.

Let me know if this works for you.

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 19, 2021, 9:03pm UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/5 "2021-07-19T21:03:20Z")

</div>

thanks Heath. I’m going to give those a try. So far the first bit of code you gave me fixing my JSON error is giving me an error in the MongoDB query field.

if I put a hardcoded date into my original query nothing changes, but it makes sense if you are saying I’m JSON was formatted incorrectly. but I can’t get either of your suggestions to work.

my mongodb setup is pretty simple. here’s the form of one of the records:

```auto
{
    "_id": {
        "$oid": "60eef##############"
    },
    "serial": "60ce###############",
    "chamber": "landfill",
    "weight": 36,
    "time": {
        "$date": "2021-07-14T15:13:08.679Z"
    }
}

```

If I use Mongo Compass or even am logged into [mongodb.com](http://mongodb.com) looking at my collection, I can use the following filter to get the results I expect: {“time”: {"$gt": ISODate(‘2021-07-17’)}}

---

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 21, 2021, 2:34am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/7 "2021-07-21T02:34:37Z")

</div>

> [@Heath](#):
>
> ```auto
> {
> “serial”: “{{data.body.serial}}”,
> “chamber”: “{{data.body.chamber}}”,
> “time”: {
> "$gt": ISODate(’{{data.body.startDate}}’)
> }
> }
> 
> ```

Hi Heath,

sorry to bother, but I still can’t get this to work. I tried copying and pasting your suggestions in just to see if it would give me progress I could build on, but your first suggestion looks like this when I put it into the mongoDB query argument box:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/1/1dbc179c1b8f1fec2744cbe413cdd55a86383fb1.png)

any suggestions?

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [July 21, 2021, 2:37am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/8 "2021-07-21T02:37:35Z")

</div>

@Tim_Brennan

My apologies. It looks like I forgot quotations around `ISODate(’{{data.body.startDate}}’)`.

So, your JSON should look like:

```auto
{
 “serial”: “{{data.body.serial}}”,
 “chamber”: “{{data.body.chamber}}”,
 “time”: {
   "$gt": “ISODate(’{{data.body.startDate}}’)”
 }
}

```

Let me know if this works.

Heath

---

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 21, 2021, 2:47am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/9 "2021-07-21T02:47:48Z")

</div>

> [@Heath](#):
>
> ```auto
> {
> “serial”: “{{data.body.serial}}”,
> “chamber”: “{{data.body.chamber}}”,
> “time”: {
> "$gt": “ISODate(’{{data.body.startDate}}’)”
> }
> }
> 
> ```

thanks Heath! definitely cleared up the json error in the module, but isn’t returning anything from mongo. I just can’t figure out why it’s not. the same query directly in mongo compass returns exactly what I want. is there a way to see the behind the scenes of what’s happening when the endpoint is hit in terms of how it’s requesting from mongo? the only different in mongo Compass is that it’s in a single line, but I’ve tried the “string template”. I’ve got the paths set for the response from mongo and the error if there is one. but with your example there is no response from mongo and no error.

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [July 21, 2021, 2:51am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/10 "2021-07-21T02:51:10Z")

</div>

@Tim_Brennan,

Have you tried the second option I’ve given?

```auto
{
  “serial”: “{{data.body.serial}}”,
  “chamber”: “{{data.body.chamber}}”,
  “time”: {
    "$gt": {{formatDate data.body.startDate 'x'}}
  }
}

```

ISODate is an option that you can only use in the MongoDB console. You will need to send just the date along in the query.

Let me know if this works!

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 21, 2021, 2:56am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/11 "2021-07-21T02:56:53Z")

</div>

> [@Heath](#):
>
> ```auto
> {
> “serial”: “{{data.body.serial}}”,
> “chamber”: “{{data.body.chamber}}”,
> “time”: {
> "$gt": {{formatDate data.body.startDate 'x'}}
> }
> }
> 
> ```

I’ll try it again now. before it didn’t return anything, but I’ll give it a shot. data.body.startDate is a date like format in my payload : “2021-07-17” for example. should the ‘x’ in the formatDate handlebar be x or should I try putting my format (YYYY-MM-DD) in for x?

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [July 21, 2021, 3:03am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/12 "2021-07-21T03:03:23Z")

</div>

@Tim_Brennan,

The ‘x’ is correct. It is a [Moment.js](https://momentjs.com/docs/#/displaying/format/) format string. You can certainly try `YYYY-MM-DD` for x instead, also, if `x` does not work.

Let me know what happens!

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 21, 2021, 3:11am UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/13 "2021-07-21T03:11:33Z")

</div>

still no results from mongo. if i try to put in ‘YYYY-MM-DD’ or that without the single quotes or that with double quotes the block errors and I can’t save. with ‘x’ it saves, but returns no results. no errors being returned either. I expect 23 records from mongo with what the data.body payload contains.

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [July 22, 2021, 3:12pm UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/14 "2021-07-22T15:12:03Z")

</div>

@Tim_Brennan,

I didn’t forget about you! 🙂

I did some testing on a collection I have. Here is what worked for me in the MongoDB Node:

```auto
{
    "createdOn": {
        "$gt": {
            "$date": "{{formatDate data.date 'x'}}"
        }
    }
}

```

So the next query you will try is:

```auto
{
  “serial”: “{{data.body.serial}}”,
  “chamber”: “{{data.body.chamber}}”,
  “time”: {
    "$gt": {
      "$date": "{{formatDate data.body.startDate 'x'}}"
    }
  }
}

```

Let me know if this works!

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Tim\_Brennan](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Tim\_Brennan](https://forums.losant.com/u/Tim_Brennan)\
**Post date:** [July 22, 2021, 7:19pm UTC](https://forums.losant.com/t/post-endpoint-workflow-mongodb-find/3936/16 "2021-07-22T19:19:52Z")

</div>

> [@Heath](#):
>
> `"{{formatDate data.body.startDate 'x'}}"`

genius. thank you Heath. I never would have gotten to the momentjs format. i thought I tried every iteration of time, date, time–\>date, everything. thank you so much for your help as always.
