It is necessary to make a selection on a certain condition, and sort the result on the previously mentioned field. How to do it?
As a driver for MongoDB using "Monk".
Assuming you have already got as far as getting a collection, then what you need is the find() method:
collection.find(query, options, callback);
You can use the query object to specify conditions, and the options object to sort. For detail on how to build the two objects see the mongodb native driver documentation.
So in your case specifically, something like this example might work. For the "condition" you mention, I'm using the condition that the "quantity" field is greater than 0, then sorting by quantity, largest first.
collection.find(
{quantity: {$gt: 0}},
{sort: {quantity: -1}},
function(err, docs) {
// docs is the sorted array of your results, process them here
});
Monk itself is fairly lightly documented, but it mostly calls through to mongoskin and then the native mongodb driver linked above, so the documentation for those is a good place to look.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With