# Need help with node-red-contrib-mongodb4

**URL:** <https://discourse.nodered.org/t/need-help-with-node-red-contrib-mongodb4/62218>\
**Category:** General\
**Tags:** function-node\
**Created:** [5 May 2022 07:36 UTC](https://discourse.nodered.org/t/need-help-with-node-red-contrib-mongodb4/62218 "2022-05-05T07:36:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ekkom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ekkom/32/61104_2.png) [@ekkom](https://discourse.nodered.org/u/ekkom)\
**Post date:** [5 May 2022 07:36 UTC](https://discourse.nodered.org/t/need-help-with-node-red-contrib-mongodb4/62218/1 "2022-05-05T07:36:07Z")

</div>

I use node-red-contrib-mongodb4 and I use find operation.  
I want to sort and limit output.  
Please show me how to use sort or limit.  
Thanks.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/337273373dffe879006404b9ccb202889c5afc52.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b4bd44b99b1e28257fb56b07f0c2c3c8fd208367.png)

---

<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:** [5 May 2022 08:28 UTC](https://discourse.nodered.org/t/need-help-with-node-red-contrib-mongodb4/62218/2 "2022-05-05T08:28:54Z")

</div>

Hello .. i did some tests with Mongodb but i used node-red-node-mongodb node instead of mongodb4  
.. i think their syntax should be more or less the same

Try to set the operation to an `aggregate` query  
and below is an example where i search between two dates and sort by descending order

```javascript
let startDate = msg.payload.startDate || "2020-01-10 00:00:00";
let endDate = msg.payload.endDate || "2021-01-15 07:00:00";

msg.collection = "temperatures"
msg.operation = "aggregate"

msg.payload = [
//{$unwind: "$temps"},
{ "$match": { "dt": { "$gte": startDate, "$lte": endDate }}},
{ "$sort": { dt: -1}},

//{"$limit": 2},
//{"$skip": 0}
];

return msg;

```

---

<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:** [4 July 2022 08:28 UTC](https://discourse.nodered.org/t/need-help-with-node-red-contrib-mongodb4/62218/3 "2022-07-04T08:28:55Z")

</div>

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