Need help with node-red-contrib-mongodb4

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

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

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;

1 Like

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