# Converting a mongo request from Robo 3T to node-red-node-mongodb

**URL:** <https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529>\
**Category:** General\
**Tags:** database\
**Created:** [11 May 2022 20:02 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529 "2022-05-11T20:02:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [11 May 2022 20:02 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/1 "2022-05-11T20:02:04Z")

</div>

Hi

I have the following request that works fine in Robo 3T:

```
db.getCollection("medias").find({deleted:true,updated_at:{$gte:ISODate("2022-04-01T00:00:00.000Z"),$lt:ISODate("2022-04-15T00:00:00.000Z")}})

```

But I don't seem to be able to convert the date-range part in Node-RED using node-red-node-mongodb

I've used a function node in front of the mongodb-in node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/9679234a7e126fec5bc892b164b0da167d665434.png)  
with:

```auto
    msg.payload = { 
        deleted: true, 
        updated_at: { 
            $gte: new Date("2022-01-01T00:00:00.000Z"), 
            $lt: new Date("2022-05-15T00:00:00.000Z") 
        } 
    }

    return msg;

```

If I only have the `deleted: true` part, the request works fine and I get all records.  
But the date-range check doesn't work  
I tried with `"$gte"` and `"$lt"` but no better luck

I've search for some mongoose sample codes as I believe the node is using that library

Any idea what I've could done wrong ?  
Thanks

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [11 May 2022 20:51 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/2 "2022-05-11T20:51:25Z")

</div>

Hello .. try wrapping your dates with the mongo $date converter  
For example :

```auto
let startDate = msg.payload.startDate || "2022-01-01T00:00:00.000Z";
let endDate = msg.payload.endDate || "2022-05-15T00:00:00.000Z";

msg.collection = "test"
msg.operation = "find"

msg.payload = { "updated_at": { "$gte": { "$date": startDate }, "$lte": { "$date": endDate } } };

return msg;

```

ps. the Mongodb nodes internally use the Mongodb Driver and not mongoose .. this may help you with your internet searches, in case there is a difference between the mongoose syntax.

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [12 May 2022 08:32 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/3 "2022-05-12T08:32:38Z")

</div>

Thanks @UnborN  
I don't know why I thought it was mongoose...  
Nevertheless I tried the following which I believe matches with your directions:

```auto
msg.payload = { 
    "deleted": true, 
    "updated_at": { 
        "$gte": {"$date": "2022-01-01T00:00:00.000Z"},
        "$lt": {"$date": "2022-05-15T00:00:00.000Z"}
    } 
}
return msg;

```

But still not items returned.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 May 2022 09:01 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/4 "2022-05-12T09:01:46Z")

</div>

hmm .. can you confirm from Robo 3T that indeed the field of `updated_at` is of type **Date** and not String  
also confirm of the `deleted` field is Boolean

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d4ceb350d6621ea5d89e895ae7d3b3c40b5efa1.png)

On further testing with some sample data, using `new Date` should have worked (it does for me)

```auto
msg.payload = { "ts": { 
"$gte": new Date("1984-03-05T13:00:00.000Z"), 
"$lte": new Date("1984-03-05T14:00:00.000Z") } 
};

```

[EDIT]  
if you can export 20-30 records from your db collection and share as a .txt or .json file on the forum so we can replicate it

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 07:06 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/5 "2022-05-13T07:06:48Z")

</div>

Hi

Regarding types, yes they are see below:  
For the dataset, let me double check that I'm allowed to share  
I will try again with `Date()` later today

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a84569f5843270ff4d79eed1ae6f9dfdd299a00e.png)

Thanks

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 07:18 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/6 "2022-05-13T07:18:29Z")

</div>

Sample data set for a little more than 30 documents  
[sampledocs.json](https://discourse.nodered.org/uploads/short-url/v7SHG7Hqid90RCHa9qkheuiuCcY.json) (32.6 KB)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 May 2022 07:37 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/7 "2022-05-13T07:37:32Z")

</div>

I loaded your sample data in a test collection and its working fine with `new Date`

**Example Flow** (produces a result of 9 records out of 37)

```auto
[{"id":"667e872dec7def34","type":"inject","z":"54efb553244c241f","name":"Inject","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":900,"wires":[["790e11e0e3f797ee"]]},{"id":"790e11e0e3f797ee","type":"function","z":"54efb553244c241f","name":"Search between dates","func":"\n\nlet startDate = new Date(\"2022-02-23T10:17:49.268+00:00\");\nlet endDate = new Date(\"2022-02-23T16:04:15.590+00:00\");\n\nmsg.collection = \"test\"\nmsg.operation = \"find\"\n\nmsg.payload = {\n \"deleted\": true,\n \"updated_at\": { \"$gte\": startDate, \"$lte\": endDate }\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"mongodb","module":"mongodb"}],"x":440,"y":900,"wires":[["0cc1e5d2b9d5ea77"]]},{"id":"0cc1e5d2b9d5ea77","type":"mongodb in","z":"54efb553244c241f","mongodb":"39adf9d0.a8c876","name":"DB","collection":"test","operation":"find","x":670,"y":900,"wires":[["6460508f49dacb36"]]},{"id":"6460508f49dacb36","type":"debug","z":"54efb553244c241f","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":900,"wires":[]},{"id":"39adf9d0.a8c876","type":"mongodb","hostname":"localhost","topology":"direct","connectOptions":"useUnifiedTopology=true","port":"27017","db":"nodered","name":""}]

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/7859453586243ace54d0189bc3ae3a7c3345b86d.png)

ps. change the mongo config settings to match your db

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 17:09 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/8 "2022-05-13T17:09:45Z")

</div>

Looks like I'm missing something  
Could you please explain why you have this ?  
I don't have it

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2a3aeb0b6e861f0badf8ab08e0345c7eb85c246.png)

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 17:14 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/9 "2022-05-13T17:14:29Z")

</div>

Am I supposed to manually install the mongodb driver ?  
shouldn't it have been installed as a dependency when installing node-red-node-mongodb

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 May 2022 17:14 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/10 "2022-05-13T17:14:42Z")

</div>

.. you can press the **x** to remove the importing of the mongodb module ..  
its not needed. i just added it when i was experimenting with the `ISODate` because that is an internal mongodb function (like ObjectId)

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 17:15 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/11 "2022-05-13T17:15:49Z")

</div>

ok, so I removed it and same result 😩

I don't understand what I could be missing  
request with only "deleted:true" is ok, it's adding the date range that breaks everything

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 17:18 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/12 "2022-05-13T17:18:48Z")

</div>

Oups sorry  
it is working with your flow.  
Great that give me some points to compare.  
Many thanks  
I'll let you know what was wrong

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 May 2022 17:21 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/13 "2022-05-13T17:21:18Z")

</div>

> [@Barbudor](#):
>
> it is working with your flow.

phew .. im glad because i was running out of ideas 😉

> [@Barbudor](#):
>
> "deleted:true"

check you if the double quotes are in the right place `"deleted" : true`

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [13 May 2022 17:29 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/14 "2022-05-13T17:29:38Z")

</div>

THe double quotes were not exact code, should have been back-quote to make it "code style"

So I don't understand because now the initial code in my initial message is working fine  
It works fine with or without quotes (looks like Javascript doesn't requires keys to be in quotes)

I don't understand what was wrong and why it now works. Will remains a big mystery.  
Thank you a lot for taking some time to help me and trying out on your own system.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f735c372dd6b8212f7aba1bb73d899d2062f4aa.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 July 2022 17:29 UTC](https://discourse.nodered.org/t/converting-a-mongo-request-from-robo-3t-to-node-red-node-mongodb/62529/15 "2022-07-12T17:29:53Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
