# Mongodb3 sort command to find maximum value

**URL:** <https://discourse.nodered.org/t/mongodb3-sort-command-to-find-maximum-value/25936>\
**Category:** General\
**Created:** [2 May 2020 16:14 UTC](https://discourse.nodered.org/t/mongodb3-sort-command-to-find-maximum-value/25936 "2020-05-02T16:14:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![divyansh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/divyansh/32/20923_2.png) [@divyansh](https://discourse.nodered.org/u/divyansh)\
**Post date:** [2 May 2020 16:14 UTC](https://discourse.nodered.org/t/mongodb3-sort-command-to-find-maximum-value/25936/1 "2020-05-02T16:14:22Z")

</div>

Hi,  
I am unable to understand, the functioning of mongodb3 package.

I am trying to get max value , using following.

```auto
msg.operation="findOne";
msg.sort={_id:-1};
msg.limit=1;
return msg;

```

However this command runs perfectly in mongodb -

`db.test.find().sort({_id:-1}).limit(1)`

return object having maximum value of \_id field.

So , please let me know, what will be equivalent of the above command in mongodb3.

Flow Code

```auto
[{"id":"a6d9fe15.99d25","type":"debug","z":"e26eb468.a89b88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1010,"y":120,"wires":[]},{"id":"dc464732.061828","type":"inject","z":"e26eb468.a89b88","name":"","topic":"","payload":"{}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"5","x":270,"y":120,"wires":[["2398a2b2.eab52e"]]},{"id":"2398a2b2.eab52e","type":"function","z":"e26eb468.a89b88","name":"","func":"msg.operation=\"findOne\";\nmsg.sort={_id:-1};\nmsg.limit=1;\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":120,"wires":[["86169ef4.09f28"]]},{"id":"86169ef4.09f28","type":"mongodb3 in","z":"e26eb468.a89b88","service":"_ext_","configNode":"63723592.a6addc","name":"","collection":"test","operation":"","x":720,"y":120,"wires":[["a6d9fe15.99d25"]]},{"id":"63723592.a6addc","type":"mongodb3","z":"","uri":"mongodb://localhost:27017/machine","name":"","options":"","parallelism":"-1"}]

```

Also

---

<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:** [1 July 2020 16:14 UTC](https://discourse.nodered.org/t/mongodb3-sort-command-to-find-maximum-value/25936/2 "2020-07-01T16:14:25Z")

</div>

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